Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Conversion failed when converting date and/or time from character string.



C#
cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO [dbo].[faculty] ([fac_id], [Fname], [Lname], [gender], [dob], [email], [country], [city], [hire_date], [salary], [commission], [fac_rank], [crs_id]) VALUES (@fac_id, @Fname, @Lname, @gender, @dob, @email, @country, @city, @hire_date, @salary, @commission, @fac_rank, @crs_id)";

            cmd.Parameters.AddWithValue("@fac_id", fac_idTextBox.Text);
            cmd.Parameters.AddWithValue("@Fname", fnameTextBox.Text);
            cmd.Parameters.AddWithValue("@Lname", lnameTextBox.Text);
            cmd.Parameters.AddWithValue("@gender", genderTextBox.Text);

            cmd.Parameters.AddWithValue("@dob", DateTime.Parse(dobDateTimePicker.Text));
            cmd.Parameters.AddWithValue("@email", emailTextBox.Text);
            cmd.Parameters.AddWithValue("@country", countryTextBox.Text);
            cmd.Parameters.AddWithValue("@city", cityTextBox.Text);

            cmd.Parameters.AddWithValue("@hire_date", DateTime.Parse(hire_dateDateTimePicker.Text));
            cmd.Parameters.AddWithValue("@salary", salaryTextBox.Text);
            cmd.Parameters.AddWithValue("@commission", commissionTextBox.Text);
            cmd.Parameters.AddWithValue("@fac_rank", fac_rankTextBox.Text);
            cmd.Parameters.AddWithValue("@crs_id", crs_idTextBox.Text);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();


What I have tried:

all the things plzz help i have submit tomorrow
Posted
Updated 2-May-16 19:49pm
Comments
George Jonsson 2-May-16 21:08pm    
On which line do you get the error?
Use the debugger and check that each of the DateTimePicker has a value.
Karthik_Mahalingam 3-May-16 1:06am    
what is the data type for dob in Table column and Stored procedure param type ?

1 solution

SQL is having a problem converting your DateTime value/s.

You should be using DateTimePicker.Value as this returns a DateTime value instead of using the Text property & converting it.

Kind Regards
 
Share this answer
 

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