divikdiya
divikdiya

Reputation: 363

how to solve Conversion failed when converting date and/or time from character string?

Conversion failed when converting date and/or time from character string

I am new in Asp.net. i want to upload image and save records in Sql Database it caught "Conversion failed when converting date and/or time from character string". I don't know why this error encountered during runtime. Anyone know help me . its very helpful my project

 protected void Add_Click(object sender, EventArgs e)
            {
                if (!FileUpload1.HasFile)
                {
                    Label2.Visible = true;
                    Label2.Text = "Please Select Image File";    //checking if file uploader has no file selected


                }
                else
                {
                    int length = FileUpload1.PostedFile.ContentLength;
                    byte[] pic = new byte[length];


                    FileUpload1.PostedFile.InputStream.Read(pic, 0, length);
                    string constr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
                    SqlConnection con = new SqlConnection(constr);
                    SqlCommand cmd1 = new SqlCommand("insert into Student" + "(RegNo,Name,DOB,Gender,Address,Country,CreatedTime,Picture) values(@RegNo,@Name,@DOB,@Gender,@Address,@Country,@date,@photo)", con);
                    con.Open();
                    cmd1.Parameters.AddWithValue("@RegNo", RegNo.Text);
                    cmd1.Parameters.AddWithValue("@Name", Name.Text);
                    cmd1.Parameters.AddWithValue("@DOB", Dob.Text);
                    cmd1.Parameters.AddWithValue("@gender", Gender.SelectedValue);
                    cmd1.Parameters.AddWithValue("@Address", Address.Text);
                    cmd1.Parameters.AddWithValue("@Country", Country.Text);
                    cmd1.Parameters.AddWithValue("@date", DateTime.Now);
                    cmd1.Parameters.AddWithValue("@photo", pic);

                    try
                    {
                        cmd1.ExecuteNonQuery();
                        Label2.Visible = true;
                        Label2.Text = "Image Uploaded Sucessfully";
                        con.Close();//after Sucessfully uploaded image
                    }
                    catch
                    {
                    }
                }


                Response.Redirect("~/WebForm1.aspx");
            }

Upvotes: 1

Views: 17471

Answers (6)

Muhammed Ali KURT
Muhammed Ali KURT

Reputation: 1

(DateTime)(object)(Dob.Text)

it's that simple :)

Upvotes: 0

paraJdox1
paraJdox1

Reputation: 973

try:

 cmd1.Parameters.AddWithValue("@date",Convert.ToDateTime(DateTime.Now.ToLongTimeString()));

It will look like this in the database:

11/12/2020 5:32:49 PM

Upvotes: 0

Nikunj Goswami
Nikunj Goswami

Reputation: 61

you can try this because it need to convert with format

cmd.Parameters.AddWithValue("@DOB",Convert.ToDateTime(emp.DOB).ToString("yyyy-MM-dd"));

Upvotes: 0

John Woo
John Woo

Reputation: 263693

I think the error is in this line:

cmd1.Parameters.AddWithValue("@DOB", Dob.Text);

You can either convert it using Convert.ToDateTime() if it is in the correct date format

cmd1.Parameters.AddWithValue("@DOB", Convert.ToDateTime(Dob.Text));

However, if it is not, there is a function called DateTime.ParseExact() which will convert specified string representation of a date and time using the specified format.

Example, your date is in format dd/MM/yy - 31/01/18

string strDob = Dob.Text; // 31/01/18
DateTime dtDob = DateTime.ParseExact(strDob , "dd/MM/yy", CultureInfo.InvariantCulture);
cmd1.Parameters.AddWithValue("@DOB", dtDob );

Upvotes: 2

sanatsathyan
sanatsathyan

Reputation: 1763

DOB might be the problem in your code, do

cmd1.Parameters.AddWithValue("@DOB", Convert.ToDateTime(Dob.Text));

make sure Dob.Text is a proper date.

Upvotes: 0

Tahsin Gül
Tahsin Gül

Reputation: 118

MS SQl's default date time format is year-month-day You need to parse your datetime format to this format Following code is

DateTime.Now.ToString("yyyy-MM-dd");

Upvotes: 0

Related Questions