In my table, I have two columns StartedDate
and EndDate
, both of them are defined as Date
datatype.
I am trying to insert values for these two columns via a textfield of type date as shown below:
<asp:Textbox type="date" ID="startDate" runat="server"></asp:Textbox>
<asp:Textbox type="date" ID="endDate" runat="server"></asp:Textbox>
I am trying to retrieve the value from these textboxes and insert them into my database.
This is my code for the insert:
// Insert a new row in the Task table
SqlDataSource1.InsertParameters["Task"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("txtTask")).Text;
SqlDataSource1.InsertParameters["StartedDate"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("startDate")).Text;
SqlDataSource1.InsertParameters["EndDate"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("endDate")).Text;
SqlDataSource1.InsertParameters["Done"].DefaultValue = ((CheckBox)GridView1.FooterRow.FindControl("DoneCbx")).Checked == true ? "true" : "false";
SqlDataSource1.InsertParameters["Priority"].DefaultValue = ((TextBox)GridView1.FooterRow.FindControl("PriorityTxt")).Text;
// Method to execute the insert
SqlDataSource1.Insert();
Insert works just fine, except for the date fields because the value retrieved from the textbox needs to be converted to DateTime
.
I tried Convert.ToDateTime
and Datetime.Parse
methods, but both times I get the following error:
Cannot implicitly convert type 'System.DateTime' to 'string'