3

I am trying to insert a row in ms-sql table using the following code

SqlCMDText = "INSERT INTO attendance(date, time_in, time_out, time_total, status,";
SqlCMDText += "  is_late_in, is_half_day, is_early_out, user_id, remarks)";
SqlCMDText += " VALUES('@yestarday_date', 'NA', 'NA', 'NA', 'ABSENT', 'NA', 'NA', 'NA', 52, 'DONE BY PORTAL')";
SqlCMD = new SqlCommand(SqlCMDText, SqlCON);
SqlCMD.Parameters.AddWithValue("@yestarday_date", Yestarday);
SqlCMD.ExecuteNonQuery();

Where Yestarday is a string variable containing short date format like "04/04/2011". I also tried passing datetime variable itself but it does not work, I get same error as below.

"Syntax Error Converting DateTime From Character String"

What format should I use to make it work?

Thanks.

1
  • your column is a datetime? so adding a parameter as a datetime should work Commented Apr 29, 2011 at 7:39

3 Answers 3

1

Use DateTime.Parse(Yestarday)

SqlCMD.Parameters.AddWithValue("@yestarday_date", DateTime.Parse(Yestarday));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Ranhiru this worked. DateTime.Parse(Yestarday). But I still don't understand why direct DateTime object is not working it should work I think.
1

You should use the DateTime object directly. If you get another error, post that one.

Comments

0

Try the following code.

CurDate = DateTime.ParseExact(@Yestarday, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.