Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
//con.Open();
                              SqlCommand cmd = new SqlCommand("sp_sales_details", con);
                              cmd.CommandType = CommandType.StoredProcedure;
                              cmd.Parameters.AddWithValue("@sd_id", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(id2) ? DBNull.Value : (object)id2;
                              cmd.Parameters.AddWithValue("@bill_no", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(bill_no) ? DBNull.Value : (object)bill_no;
                              cmd.Parameters.AddWithValue("@itemcode", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(itm_code) ? DBNull.Value : (object)itm_code;
                              cmd.Parameters.AddWithValue("@item", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(item) ? DBNull.Value : (object)item;
                              cmd.Parameters.AddWithValue("@unit", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(unitprice) ? DBNull.Value : (object)unitprice;
                              cmd.Parameters.AddWithValue("@qty", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(qty1) ? DBNull.Value : (object)qty1;
                              cmd.Parameters.AddWithValue("@amount", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(amt) ? DBNull.Value : (object)amt;
                              cmd.Parameters.AddWithValue("@date", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(date) ? DBNull.Value : (object)date;
                              cmd.Parameters.AddWithValue("@time", SqlDbType.Time).Value = string.IsNullOrWhiteSpace(time) ? DBNull.Value : (object)time;
                              cmd.Parameters.AddWithValue("@sales_id", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(sales_id) ? DBNull.Value : (object)sales_id;
                              cmd.Parameters.AddWithValue("@employee_id", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(txtCmboSelCaptain.SelectedValue.ToString()) ? DBNull.Value : (object)txtCmboSelCaptain.SelectedValue.ToString();
                              cmd.Parameters.AddWithValue("@shift", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(shift.ToString()) ? DBNull.Value : (object)shift.ToString();
                              cmd.Parameters.AddWithValue("@tables", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(tables) ? DBNull.Value : (object)tables;
                              cmd.Parameters.AddWithValue("@chair", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(chair) ? DBNull.Value : (object)chair;
                              cmd.Parameters.AddWithValue("@remarks", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(remarks) ? DBNull.Value : (object)remarks;
                              cmd.Parameters.AddWithValue("@unit_type", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(unit_type) ? DBNull.Value : (object)unit_type;
                              cmd.Parameters.AddWithValue("@ser_tax", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(ser_tax) ? DBNull.Value : (object)ser_tax;
                              cmd.Parameters.AddWithValue("@var_tax", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(vat_tax) ? DBNull.Value : (object)vat_tax;
                              cmd.Parameters.AddWithValue("@service", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(service) ? DBNull.Value : (object)service;
                              cmd.Parameters.AddWithValue("@vat", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(vat) ? DBNull.Value : (object)vat;
                              cmd.Parameters.AddWithValue("@total_amount", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(total) ? DBNull.Value : (object)total;
                              cmd.Parameters.AddWithValue("@cat", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(cat) ? DBNull.Value : (object)cat;
                              cmd.Parameters.AddWithValue("@sales_type", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(sales_type) ? DBNull.Value : (object)sales_type;
                              cmd.Parameters.AddWithValue("@captain_id", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(txtCmboSelCaptain.SelectedValue.ToString()) ? DBNull.Value : (object)txtCmboSelCaptain.SelectedValue.ToString();
                              cmd.Parameters.AddWithValue("@kot_no", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(kot.ToString()) ? DBNull.Value : (object)kot.ToString();
                              cmd.Parameters.AddWithValue("@kot_status", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(kot_status) ? DBNull.Value : (object)kot_status;
                              cmd.ExecuteNonQuery();
                              con.Close();
                              MessageBox.Show("Inserted Successfully", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);


What I have tried:

//con.Open();
                              SqlCommand cmd = new SqlCommand("sp_sales_details", con);
                              cmd.CommandType = CommandType.StoredProcedure;
                              cmd.Parameters.AddWithValue("@sd_id", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(id2) ? DBNull.Value : (object)id2;
                              cmd.Parameters.AddWithValue("@bill_no", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(bill_no) ? DBNull.Value : (object)bill_no;
                              cmd.Parameters.AddWithValue("@itemcode", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(itm_code) ? DBNull.Value : (object)itm_code;
                              cmd.Parameters.AddWithValue("@item", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(item) ? DBNull.Value : (object)item;
                              cmd.Parameters.AddWithValue("@unit", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(unitprice) ? DBNull.Value : (object)unitprice;
                              cmd.Parameters.AddWithValue("@qty", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(qty1) ? DBNull.Value : (object)qty1;
                              cmd.Parameters.AddWithValue("@amount", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(amt) ? DBNull.Value : (object)amt;
                              cmd.Parameters.AddWithValue("@date", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(date) ? DBNull.Value : (object)date;
                              cmd.Parameters.AddWithValue("@time", SqlDbType.Time).Value = string.IsNullOrWhiteSpace(time) ? DBNull.Value : (object)time;
                              cmd.Parameters.AddWithValue("@sales_id", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(sales_id) ? DBNull.Value : (object)sales_id;
                              cmd.Parameters.AddWithValue("@employee_id", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(txtCmboSelCaptain.SelectedValue.ToString()) ? DBNull.Value : (object)txtCmboSelCaptain.SelectedValue.ToString();
                              cmd.Parameters.AddWithValue("@shift", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(shift.ToString()) ? DBNull.Value : (object)shift.ToString();
                              cmd.Parameters.AddWithValue("@tables", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(tables) ? DBNull.Value : (object)tables;
                              cmd.Parameters.AddWithValue("@chair", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(chair) ? DBNull.Value : (object)chair;
                              cmd.Parameters.AddWithValue("@remarks", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(remarks) ? DBNull.Value : (object)remarks;
                              cmd.Parameters.AddWithValue("@unit_type", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(unit_type) ? DBNull.Value : (object)unit_type;
                              cmd.Parameters.AddWithValue("@ser_tax", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(ser_tax) ? DBNull.Value : (object)ser_tax;
                              cmd.Parameters.AddWithValue("@var_tax", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(vat_tax) ? DBNull.Value : (object)vat_tax;
                              cmd.Parameters.AddWithValue("@service", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(service) ? DBNull.Value : (object)service;
                              cmd.Parameters.AddWithValue("@vat", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(vat) ? DBNull.Value : (object)vat;
                              cmd.Parameters.AddWithValue("@total_amount", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(total) ? DBNull.Value : (object)total;
                              cmd.Parameters.AddWithValue("@cat", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(cat) ? DBNull.Value : (object)cat;
                              cmd.Parameters.AddWithValue("@sales_type", SqlDbType.VarChar).Value = string.IsNullOrWhiteSpace(sales_type) ? DBNull.Value : (object)sales_type;
                              cmd.Parameters.AddWithValue("@captain_id", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(txtCmboSelCaptain.SelectedValue.ToString()) ? DBNull.Value : (object)txtCmboSelCaptain.SelectedValue.ToString();
                              cmd.Parameters.AddWithValue("@kot_no", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(kot.ToString()) ? DBNull.Value : (object)kot.ToString();
                              cmd.Parameters.AddWithValue("@kot_status", SqlDbType.Int).Value = string.IsNullOrWhiteSpace(kot_status) ? DBNull.Value : (object)kot_status;
                              cmd.ExecuteNonQuery();
                              con.Close();
                              MessageBox.Show("Inserted Successfully", "Fill Field", MessageBoxButton.OK, MessageBoxImage.Information);
Posted
Updated 4-Dec-22 18:34pm
Comments
Graeme_Grant 4-Dec-22 23:46pm    
You have asked over 30 questions in the last couple of months. You should know by now how to post the code and discuss your issue correctly. We are not here to debug your code for you.

Error converting data type nvarchar to int = can't convert string to integer. Find the line of code that has this error and fix the conversion error.

1 solution

The problem is simple: you need to validate your data.
You are blindly passing strings (the source of which we have no idea) to SQL to enter into INT fields. If the source does not provide a valid (but non-empty) numeric string (like "12.34" for example) then SQL tries to convert it to an integer, fails, and throws an error.

Validate your data by converting to to numbers in your presentation code and pass the integer value directly to SQL. You should (after 30+ questions) know how to do basic stuff like this yourself!
 
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