-1

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'

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Khuzama
  • 41
  • 6
  • Please show us the `CREATE TABLE` script for the table. – mjwills Nov 30 '20 at 04:08
  • @mjwills CREATE TABLE [dbo].[TaskTable] ( [ToDoId] INT IDENTITY (1, 1) NOT NULL, [Task] VARCHAR (250) NOT NULL, [StartedDate] DATE NULL, [EndDate] DATE NULL, [Done] BIT NULL, [Priority] INT NOT NULL, PRIMARY KEY CLUSTERED ([ToDoId] ASC) ); – Khuzama Nov 30 '20 at 04:19
  • With the code **as is in your question** - a) does it compile sucessfully? b) does it run? c) what **exactly** isn't working with it? – mjwills Nov 30 '20 at 04:39
  • @mjwills If I don't add values for start date and end date ( they can be null ) then it works fine. But I cannot insert the date values, if I do, it throws an exception: system.formatexception string was not recognized as a valid datetime on line 32 which is the SqlDataSource1.Insert() – Khuzama Nov 30 '20 at 04:49
  • Do what the below answer says - but call `ToString` on the result like in the duplicate. To be clear - you should avoid the use of `SqlDataSource` since it is crap (one of the reasons it is crap is the issue you just ran into). – mjwills Nov 30 '20 at 04:53

1 Answers1

0

DateTime.Parse method:

SqlDataSource1.InsertParameters["StartedDate"].DefaultValue = DateTime.Parse(((TextBox)GridView1.FooterRow.FindControl("startDate")).Text);
Backs
  • 24,430
  • 5
  • 58
  • 85
  • I did try that as mentioned in the question post. I got the following error: Error CS0029 Cannot implicitly convert type 'System.DateTime' to 'string' – Khuzama Nov 30 '20 at 04:18