Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one exception (Conversion failed when converting date and/or time from character string.) 

how to convert this please help me.

This my coding


C#
protected void Save(object sender, EventArgs e)
  {

      conn();

      string ImgSmall = Path.GetFileName(fileuploadimgsmall.PostedFile.FileName);
      fileuploadimgsmall.SaveAs(Server.MapPath("ImagesSmall/" + ImgSmall));

      string ImgLarge = Path.GetFileName(fileuploadimglarge.PostedFile.FileName);
      fileuploadimglarge.SaveAs(Server.MapPath("ImagesLarge/" + ImgLarge));

      objcmd = new SqlCommand("Insert into TBL_PDT_IMG(PDT_ID,IMG_NAME,IMG_DESC,PDT_IMAGE_SMALL,PDT_IMAGE_LARGE,IS_PRIMARY,Created_by,Created_dt,Changed_by,Changed_dt) values(@PDT_ID,@IMG_NAME,@IMG_DESC,@PDT_IMAGE_SMALL,@PDT_IMAGE_LARGE,@IS_PRIMARY,@Created_by,@Created_dt,@Changed_by,@Changed_dt)", objcon.con);

      objcmd.Parameters.AddWithValue("@PDT_ID", ddlproduct.SelectedValue);
      objcmd.Parameters.AddWithValue("@IMG_NAME", txtimgname.Text.Trim());
      objcmd.Parameters.AddWithValue("@IMG_DESC", txtimgdesc.Text.Trim());
      objcmd.Parameters.AddWithValue("@PDT_IMAGE_SMALL", "ImagesSmall/" + ImgSmall);
      objcmd.Parameters.AddWithValue("@PDT_IMAGE_LARGE", "ImagesLarge/" + ImgLarge);
      objcmd.Parameters.AddWithValue("@IS_PRIMARY", chkstatus.Checked);
      objcmd.Parameters.AddWithValue("@Created_by", "RG");
      objcmd.Parameters.AddWithValue("@Created_dt",DateTime.Now.ToString());
      objcmd.Parameters.AddWithValue("@Changed_by", ',');
      objcmd.Parameters.AddWithValue("@Changed_dt", ',');
      objcmd.ExecuteNonQuery();

      bind();

}
Posted
Comments
Maciej Los 7-Mar-14 2:09am    
Not clear! What kind of issue do you have? What error message?

@Created_dt parameter inserts value into Created_dt field of your table which most likely is a datetime field.

Just pass DateTime.Now instead of DateTime.Now.ToString() in this line:
C#
objcmd.Parameters.AddWithValue("@Created_dt",DateTime.Now.ToString());

and it should be fine.
 
Share this answer
 
Comments
jeevakumar.T 7-Mar-14 2:32am    
Thank you Friend
Ankur\m/ 7-Mar-14 4:13am    
You may vote up the answer if it helped.
jeevakumar.T 7-Mar-14 6:47am    
ya it helped.i used DateTime.Now value and null value also used.it worked correctly.
thank by jeeva
Ankur\m/ 7-Mar-14 6:54am    
So you can up-vote the answer then.
1) it might be because of many reasons one is the Data input
2) the date cannot be ','
3) instead pass null or default datetime 1900-01-01

change it to this and give a try
**********************
objcmd.Parameters.AddWithValue("@Changed_dt", null);
 
Share this answer
 
Comments
jeevakumar.T 7-Mar-14 2:32am    
Thank you Friend
Ankur\m/ 7-Mar-14 4:12am    
That may not be correct. By the parameter name it seems that user want to save details of last change date and so he is passing DateTime.Now. Converting it to string is what is giving him the error. Passing NULL is not a good solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900