0

I am new to C#, and learning. I have a SQL Server database where I have table record that has a column dob of data type date. I am trying to insert into dob from my textbox which has a value of 19-08-17 with the following code

datebox.Text = "19-08-17";

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlCommand com = new SqlCommand(query, con);
string query= "insert into record (dob) values(@db)";
com.Parameters.AddWithValue("@db", datebox.Text);
com.ExecuteNonQuery();

But I get this error

Conversion failed when converting date and/or time from character string.

How can I solve this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Self-explanatory! Since your column type in the database is date, Convert `datebox.Text` to date format while setting the parameter. – Raging Bull Aug 19 '17 at 12:35
  • Possible duplicate of [Conversion failed when converting date and/or time from character string while inserting datetime](https://stackoverflow.com/questions/14119133/conversion-failed-when-converting-date-and-or-time-from-character-string-while-i) – Hina Khuman Aug 19 '17 at 12:36
  • 1
    Error is explaining it self : Typecast error, try this: DateTime.Parse(datebox.Text, CultureInfo.CurrentCulture); – Parveen Sachdeva Aug 19 '17 at 12:37

1 Answers1

0

When you pass a variable to a DateTime column as a parameter, you need to convert the text to a DateTime variable before passing it:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].Connect    ionString);

// Convert the textbox text to a DateTime variable
DateTime myDate = DateTime.ParseExact(datebox.Text, "dd-MM-yy", System.Globalization.CultureInfo.InvariantCulture);

SqlCommand com = new SqlCommand(query, con);
string query= "insert into record (dob) values(@db)";
com.Parameters.AddWithValue("@db", myDate);
com.ExecuteNonQuery();
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
  • wow its working fine now. i want to ask one more thing from you – rahul yadav Aug 19 '17 at 12:40
  • yes am waiting system saying wait 5 minutes to accept answer, – rahul yadav Aug 19 '17 at 12:43
  • No problem, what do you want to ask? – Koby Douek Aug 19 '17 at 12:44
  • i want to be professional programmer like others and you, how i can ? am good learner if i have good guideline – rahul yadav Aug 19 '17 at 12:44
  • @rahulyadav It's just a matter of a lot of practice and writing as much code as you can. First, take a good C# tutorial online, there are so many of them. Then, try writing as many programs and websites as you can. It will come with time, with experience. Nobody is born being a good programmer. Also, read many posts as possible on this site, and even try answering some questions yourself, even if you are not sure. – Koby Douek Aug 19 '17 at 12:47
  • ok thanks but am confused which language should i learn fully, some time learn php, some tim android development and now am learning asp.net with c#. inshort have no one path – rahul yadav Aug 19 '17 at 12:52
  • 1
    @rahulyadav That's not good. You have to stick with 1 programming language until you feel like you controls it completely. I suggest you stick with ASP.NET and C#, while using Javascript on the client side. This will provide you an entire world of capabilities. – Koby Douek Aug 19 '17 at 12:54