从文本框中获取日期并将其存储在datatable中

时间:2013-04-08 01:22:57

标签: c# asp.net gridview datatable

我在asp.net中有一个文本框控件,我在使用jquery datepicker并在textbox中插入日期。现在我想将这个日期添加到Datatable>我试过但它给出错误作为对象引用未设置为对象的实例。

protected void btnbook_Click(object sender, EventArgs e)
    {

        DataTable dtable = new DataTable();
        dtable.Columns.Add(new System.Data.DataColumn("ID", typeof(int)));
        dtable.Columns.Add(new System.Data.DataColumn("CarName", typeof(String)));
        dtable.Columns.Add(new System.Data.DataColumn("Price", typeof(int)));
        dtable.Columns.Add(new System.Data.DataColumn("Start Date", typeof(String)));
        dtable.Columns.Add(new System.Data.DataColumn("End Date", typeof(String)));
        int i = 0;
        foreach (GridViewRow gvrow in gvcar.Rows)
        {
            if (((CheckBox)gvrow.FindControl("chkSelect")).Checked == true)
            {
                var dataKey = gvcar.DataKeys[gvrow.RowIndex];
                if (dataKey != null)
                {
                    Label CarName = (Label)gvrow.FindControl("carname");
                    Label CarPrice = (Label)gvrow.FindControl("price");
                    TextBox startdate = (TextBox)Page.FindControl("startdate");
                    TextBox enddate = (TextBox)Page.FindControl("enddate");
                    //string CarName = gvrow.Cells[1].Text;
                    //int Price = int.Parse(gvrow.Cells[4].Text.ToString());

                    DataRow dr = dtable.NewRow();
                    dr[0] = dataKey.Value;
                    dr[1] = CarName.Text;
                    dr[2] = Convert.ToInt32(CarPrice.Text);
                    dr[3] = startdate.Text;
                    dr[4] = enddate.Text;
                    dtable.Rows.Add(dr);

                }

            }
        }

0 个答案:

没有答案