Rahul Lakshakar

Rahul Lakshakar

  • NA
  • 199
  • 41.3k

Insert datetime into databse

Oct 15 2016 12:19 PM
string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
string stime = Text_Stime.Text;
string etime = Text_Etime.Text;
DateTime Sdatetime = new DateTime();
Sdatetime = DateTime.ParseExact(Text_Stime.Text, "yyyy-MM-dd hh:mm tt", null);
DateTime Edatetime = new DateTime();
Edatetime = DateTime.ParseExact(Text_Etime.Text, "yyyy-MM-dd hh:mm tt", null);
string insertData = "insert into ExamDetail (ExamName, SubjectName, Duration, StartTime, EndTime, NegativeMark, QusNo, QusMarks) values (@EName, @SubName, @DUR, @ST, @ET, @NM, @QN,@QM)";
SqlCommand com = new SqlCommand(insertData, con);
com.Parameters.AddWithValue("@EName", DropExamName.SelectedItem.Text);
com.Parameters.AddWithValue("@SubName", DropSubName.SelectedItem.Text);
com.Parameters.AddWithValue("@DUR", DropDuration.SelectedItem.Text);
com.Parameters.AddWithValue("@ST", Sdatetime);
com.Parameters.AddWithValue("@ET", Edatetime);
com.Parameters.AddWithValue("@NM", RadioNegativeMarks.SelectedItem.Text);
com.Parameters.AddWithValue("@QN", Text_question_no.Text);
com.Parameters.AddWithValue("@QM", text_question_marks.Text);
com.ExecuteNonQuery();
I am trying to insert datetime into database. But it showing an error "String was not recognized as a valid DateTime." I am using above code. I am using textbox's TextMode="DateTimeLocal".
Please Help me...How can i remove this error? 

Answers (6)