am using C#, VS 2005 and SQL 2000
I have date conversion problem my SQL table having field as below
Fields:
datefrom
dateto
My SQL query in C# is
string sql = "insert into companymast (cname, datefrom, dateto) values(@cname, @datefrom, @dateto)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@cname",SqlDbType.Varchar(50)).Values = cname.text;
cmd.Parameters.Add("@datefrom",SqlDbType.Datetime).Values = maskedTextBox1.text;
cmd.Parameters.Add("@dateto",SqlDbType.Datetime).Values = maskedTextBox2.text;
cmd.ExecuteNonQuery();
But the above throw Error Like date non conversion string to date
I have to put date in dd/MM/yyyy format code, 103
so how do i do?
Help me out, please.
maskedBox1.textis a string. You’re assigning that to aSqlParameter‘s data member, and it tries to convert whatever it receives to the data type you request.A more solid solution would be to convert to
DateTimeexplicitly, and pass the converted value to theSqlParameter. This way, you have more control over the conversion, and you’ll see exactly where and why it goes wrong when it does. Example:For DateTime.Parse, see: http://msdn.microsoft.com/en-us/library/1k1skd40.aspx