1

i have SQL server database and table with column for date (date datatype). I would like to change the value of date column for all records to have todays date, but something is not working, please help. My code:

 con.Open();
 DateTime date = DateTime.Now.Date;
 string insStr = "UPDATE tblBusiness SET DateCreated = @Date;";
 SqlCommand updateDate = new SqlCommand(insStr, con);
 updateDate.Parameters.AddWithValue("Date", date);
 updateDate.ExecuteNonQuery();
 con.Close();

Thanks, Oak

EDIT: Thanks for the answers. The thing why it was not working was because of too many records in database and the default update timeout is 30sec or something. And thats why no record was changed. updateDate.CommandTimeout = 0; fixed the problem.

1
  • Thanks for the answers. The thing why it was not working was because of too many records in database and the default update timeout is 30sec or something. And thats why no record was changed. updateDate.CommandTimeout = 0; fixed the problem. Commented May 11, 2012 at 14:39

7 Answers 7

5

I think this line:

updateDate.Parameters.AddWithValue("Date", date);

Should be:

updateDate.Parameters.AddWithValue("@Date", date);
Sign up to request clarification or add additional context in comments.

Comments

2

use @Date instead of Date

updateDate.Parameters.AddWithValue("@Date", date);

Comments

1
//Replace
updateDate.Parameters.AddWithValue("Date", date);

// with (watch the @-symbol)
updateDate.Parameters.AddWithValue("@Date", date);

Comments

1

try

updateDate.Parameters.AddWithValue("@Date", date);

Comments

0

Replace Date with @Date.

updateDate.Parameters.AddWithValue("@Date", date);

Comments

0

Thanks for the answers. The thing why it was not working was because of too many records in database and the default update timeout is 30sec or something. And thats why no record was changed. updateDate.CommandTimeout = 0; fixed the problem.

Comments

-1

This should work. You don't need a where clause if you are updating all records.

con.Open();
 DateTime date = DateTime.Now.Date;
 string insStr = "UPDATE tblBusiness SET DateCreated = " + date;
 SqlCommand updateDate = new SqlCommand(insStr, con);
 updateDate.ExecuteNonQuery();
 con.Close();

2 Comments

You had better have one (unless you want to update all records)
You guys are correct. I misread the question. But I have revised my answer.

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.