Click here to Skip to main content
15,888,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my code if anyone can help me please. I have created a booking ASP.NET Web Application 4.5 and are having problems on the admin page where the admin user can create a manual booking. Below are two errors i am having.

1: Conversion failed when converting the nvarchar value 'Full' to data type int.
2:Cannot convert char value to smallmoney. The char value has incorrect syntax.

here is my code where it is failling.

C#
protected void Button1_Click1(object sender, EventArgs e)
    {
        // make a new job record and insert the data from the form - if there is no Cist_id it has to go in as a null

        SqlCommand cmd = new SqlCommand("INSERT INTO Job( Date, Start_Time, Valet_ID, Additions, Car_Reg, Worker_ID, Comments, Cust_ID) Values(@Date, @Start_Time, @Valet_ID, @Additions, @Car_Reg, @Worker_ID, @Comments, @Cust_ID)", con);
        cmd.Parameters.AddWithValue("@Date", SqlDbType.Date).Value = txtDate.Text;
        cmd.Parameters.AddWithValue("@Start_Time", SqlDbType.Time).Value = DropTime.Text;
        cmd.Parameters.AddWithValue("@Valet_ID", txtValetType.Text);
        cmd.Parameters.AddWithValue("@Additions", txtAdditions.Text);
        cmd.Parameters.AddWithValue("@Car_Reg", txtCarReg.Text);
        cmd.Parameters.AddWithValue("@Worker_ID", txtWorker_ID.Text);
        cmd.Parameters.AddWithValue("@Comments", txtComments.Text);
        cmd.Parameters.AddWithValue("@Cust_ID", txtCust_ID.Text);

        txtDate.Text = Convert.ToString(Calendar1.SelectedDate);
        txtDate.Text = Calendar1.SelectedDate.ToShortDateString();
        DropTime.SelectedValue = Convert.ToString(DropTime);
        txtValetType.Text = Convert.ToString(txtValetType);
       

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



Kind Regards
Neville

What I have tried:

have tried examples on-line to convert the date and time to a string. I pick a date from the calendar and it populates a text box with the selected date. The time is populated by Times that I have manually entered using collection, and is selected according to the customers required time. The times are from 0800hrs to 1930hrs. The times are split avery half an hour, for example 0800 -0830-0900-0930 and so on, and the last one stipulates that a express Valet is only available, for example 19:30 Express Only. I am not sure if the string Express Only is causing any issues but I cant see any at the moment.

If anyone could look at my code and help me with the errors I am having , I would be most grateful.
Posted
Updated 31-Mar-16 5:10am
v3
Comments
ZurdoDev 31-Mar-16 10:35am    
First error is very simple. You are passing the string value "Full" to a parameter that is Integer.
Second issue is nearly the same.

Just debug your code and you'll see the values you are passing. Should be very easy to fix.
Karthik_Mahalingam 31-Mar-16 10:40am    
both are casting error in sql
you have to validate in front end before passing it to the SQL Database.
neville1967 5-Apr-16 9:51am    
Many thanks for your help

The value you provide for SqlParameters has to be of the type that you specified for it. If you have a SqlParameter with SqlDbType.Date then you need to provide a value of type DateTime, but you provided a string.

For SqlDbType.Time you also need to provide a value of type DateTime.

For the other parameters you didn't specify a SqlDbType, but that doesn't relieve you of having to provide an object of the correct type. For integer-columns you have to provide an integer value and so on.

You should use the appropriate .Parse(..) or .TryParse(..) methods of the according types to parse the string value into an object of the correct type, e.g.:
C#
DateTime date;
if (!DateTime.TryParse(txtDate.Text, out date))
{
   // handle conversion error (e.g. show error message)
}

// ...

cmd.Parameters.AddWithValue("@Date", SqlDbType.Date).Value = date;

// ...

Basically the same goes for integers and other types.
 
Share this answer
 
v2
Comments
neville1967 5-Apr-16 9:52am    
Many thanks for your help, all sorted now
Sascha Lefèvre 5-Apr-16 17:00pm    
You're welcome!
First of all, never store numeric data in varchar. Use numeric SQL data types for this purpose.

Sometimes, you have no choice and have to interpret string data as numeric. In this case, never use Convert.ToString. It may (or may not) work, but… this is not really conversion. This is string parsing, successful or not. Better use int.Parse, or better int.TryParse (the second one not throwing an exception), as well as the methods under the same names of other types:
Int32.GetTypeCode Method (System)[^].

—SA
 
Share this answer
 
Comments
neville1967 5-Apr-16 9:52am    
Many thanks for your help, all sorted now
Sergey Alexandrovich Kryukov 5-Apr-16 10:10am    
You are welcome.
Are you going to accept the answer formally?
—SA

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