问题描述:

Invalid cast from 'Int32' to 'DateTime'.Couldn't store <1> in Tdate Column. Expected type is DateTime.

My query is as below

Update LogMail set Tdate= '20-Jul-15 12:00:00 AM'

and my code is given below

 DateTime dtCurrTime = DateTime.Today;

string d = dtCurrTime.ToString();

string sQuery1 = "Update LogMail set Tdate= '"+Convert.ToDateTime(d)+"' ";

DBInteraction.DBOperation.sConnectionString = GlobalFuncs.sConnectionString;

sRetVal = DBOperation.ExecuteDBOperation(sQuery1, DBOperation.OperationType.UPDATE, null, ref dt);

if (sRetVal == GlobalFuncs.SUCCESS)

{

Challan.EmailForPandingMaterial em=new Challan.EmailForPandingMaterial();

em.ShowDialog();

this.Hide();

}

there is class DBinteraction for database and DBOperation is its method and it works fine in all other program.Please help me to find right couse of error. Thanks

网友答案:

Try using DateTime.Now

string sQuery1 = "Update LogMail set Tdate= '"+DateTime.Now+"' ";

see if it generates the same error. check your datatype in Database to make sure it is of correct datatype.

Or try to save date in following date format as this is the default format in MS SQL 'yyyy-MM-dd'

网友答案:
// any custom format depending on your needs:
string d = dtCurrTime.ToString("yyyy-MM-dd HH:mm:ss"); 

string sQuery1 = "Update LogMail set Tdate= '"+d+"' ";
相关阅读:
Top