Here is the code:
string ConnectionString= @"Data Source=localhost\SQLEXPRESS;
Initial Catalog=notepad; Integrated Security=SSPI ";
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
string strEvent = TextBoxEvent.Text;
string strDate = Calendar1.TodaysDate.ToShortDateString();
string strInsert = "insert into notepad (time, event) values (strDate, strEvent )";
SqlCommand cmd=new SqlCommand(strInsert, con);
cmd.ExecuteNonQuery();
the time is smalldatetime
in SQL Server 2005
When I run this program, an error occurrs like this:
The name "strDate" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
but if I replace the strDate
with 2010/05/22
like this:
string strInsert = "insert into notepad (time, event) values ("2010/05/22", strEvent )";
the program will run correctly.
I am puzzled with this problem and turn for help to you.