Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void FRM_RPT_ONE_CHANGE_TIRE_Load(object sender, EventArgs e)
       {
           s = "SELECT s1.Car_id ,s1.Job_card ,s1.value_tire ,convert(varchar,s1.Change_date,101) as Change_date ,s1.Current_dist, s1.Prev_dist ,(s1.Current_dist - s1.Prev_dist) as Change_dist , Drivers.Driver_Name, Tire_type.Tire_desc, s1.Tire_size, Providers.Provider_desc, Front_R ,Front_L,Rare_R, Rare_L ,s1.Notes FROM Change_Tire s1  INNER JOIN Drivers ON s1.Driver_ID = Drivers.Driver_ID INNER JOIN Tire_type ON s1.Tire_id = Tire_type.Tire_id  INNER JOIN Providers ON s1.Provider_id = Providers.Provider_id where s1.Car_id = " + x + " and s1.Job_card = " + y;
           DataSet_One_Change_Tire ds = new DataSet_One_Change_Tire();
           SqlDataAdapter dataAdapter = new SqlDataAdapter(s, con);
            dataAdapter.Fill(ds.Tables["Change_Tire"]);
           CrystalReport_One_Change_tire report = new CrystalReport_One_Change_tire();
           report.SetDataSource(ds.Tables["Change_Tire"]);
           crystalReportViewer_One_Change_tire.ReportSource = report;
           crystalReportViewer_One_Change_tire.Refresh();
       }
Posted

1 solution

You can't do a mathematical operation on strings. In most cases SQL Server will try an implicit type conversion but if the fields contain NULL or something that can't be converted to a numerical value you'll get the above error. Change the datatype of Current_dist and Prev_dist to a numeric datatype like int or decimal and it will work.

If you want to keep varchar datatype, you'll have to ensure that all values in your table can be converted to a numeric value. The simpliest thing would be to set default for NULL values like:

CAST(IsNULL(sq.Current_dist, '0') as int)

This won't prevent from strings that can't be converted but if your values are all convertable, this will work too.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Jan-16 15:36pm    
I can only say "you can't do arithmetic operations on string", and even that only if "arithmetic" is understood in narrow sense of this word. "Mathematical" is too broad notion, anything can be "mathematical".
—SA
NightWizzard 25-Jan-16 15:42pm    
Thanks Sergey, you're right and that's what I meant but couldn't find the right word that moment.

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