Click here to Skip to main content
15,909,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
try
                {
                    

                    SqlDataAdapter daa = new SqlDataAdapter("select max(idcat) from category", con);
                    DataSet ds = new DataSet();
                    daa.Fill(ds, "category");
                    DataView dvv = new DataView(ds.Tables["category"]);


                    DateTime date = System.Convert.ToDateTime(dateTimePicker1.Text);
                    int numsup;
                    numsup = int.Parse(comboBoxSppNo.Text);
                    //MessageBox.Show(numsup.ToString());
                    SqlCommand cmd3 = new SqlCommand("exec insertparts @partnumber,@description,@date,@quantity,@unitPrice,@AddnewQty,untpnewQty,@vat,@suppId", con);                    
                    cmd3.Parameters.AddWithValue("@partnumber",txtPartNo.Text);
                    cmd3.Parameters.AddWithValue("@description", txtDesc.Text);
                    cmd3.Parameters.AddWithValue("@date", date);
                    cmd3.Parameters.AddWithValue("@quantity", txtEntrance.Text);
                    cmd3.Parameters.AddWithValue("@unitPrice", txtunitprice.Text);
                    cmd3.Parameters.AddWithValue("@AddnewQty", 0);
                    cmd3.Parameters.AddWithValue("@untpnewQty", 0);
                    cmd3.Parameters.AddWithValue("@vat", txtvat.Text);
                   // cmd3.Parameters.AddWithValue("@desccat", txtDesc.Text);
                    cmd3.Parameters.AddWithValue("@suppId", numsup);
                    


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

                    MessageBox.Show("SUCCESSFULL INSERT");
Posted
Comments
CHill60 15-Jan-14 7:18am    
Should there be a "@" in front of untpnewQty on the cmd3 assignment?

Well, this is just a guess, but I'd suggest that you need to look at what your user typed in...

The error message means that you are trying to insert a value to an integer column, but the string you gave (presumably from one of the text boxes) is not recognisable as a number - it may be blank, or contain letters for example.

What you should do is check each textbox at the top of the method by converting it to the appropriate datatype and reporting an error if he got it wrong, rather than proceeding to do the insert. You then use the converted value in your SqlParameter to pass it directly to SQL.
 
Share this answer
 
I guess from the below Columns, one is Integer and you are trying to insert String.
Please cross check the DataType against the Database. They should match.
C#
cmd3.Parameters.AddWithValue("@partnumber",txtPartNo.Text);
cmd3.Parameters.AddWithValue("@description", txtDesc.Text);
cmd3.Parameters.AddWithValue("@quantity", txtEntrance.Text);
cmd3.Parameters.AddWithValue("@unitPrice", txtunitprice.Text);
cmd3.Parameters.AddWithValue("@vat", txtvat.Text);
 
Share this answer
 
You should cross check few points:
1. Your parameter sequence in C# code and store procedure sequence should be same.
2. Your stored procedure input parameter data type and C# code parameter data type should be same.
3. Your parameter data type that is inserting and column data type should be same.

And do some change in c3 code for stored procedure:
C#
try
            {


                SqlDataAdapter daa = new SqlDataAdapter("select max(idcat) from category", con);
                DataSet ds = new DataSet();
                daa.Fill(ds, "category");
                DataView dvv = new DataView(ds.Tables["category"]);


                DateTime date = System.Convert.ToDateTime(dateTimePicker1.Text);
                int numsup;
                numsup = int.Parse(comboBoxSppNo.Text);
                //MessageBox.Show(numsup.ToString());
                SqlCommand cmd3 = new SqlCommand("insertparts", con);
                cmd3.CommandType = CommandType.StoredProcedure;
                cmd3.Parameters.AddWithValue("@partnumber", txtPartNo.Text);
                cmd3.Parameters.AddWithValue("@description", txtDesc.Text);
                cmd3.Parameters.AddWithValue("@date", date);
                cmd3.Parameters.AddWithValue("@quantity", txtEntrance.Text);
                cmd3.Parameters.AddWithValue("@unitPrice", txtunitprice.Text);
                cmd3.Parameters.AddWithValue("@AddnewQty", 0);
                cmd3.Parameters.AddWithValue("@untpnewQty", 0);
                cmd3.Parameters.AddWithValue("@vat", txtvat.Text);
                // cmd3.Parameters.AddWithValue("@desccat", txtDesc.Text);
                cmd3.Parameters.AddWithValue("@suppId", numsup);



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

                MessageBox.Show("SUCCESSFULL INSERT");
            }
 
Share this answer
 
Try like this..

C#
SqlCommand cmd3 = new SqlCommand("insertparts", con);
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.AddWithValue("@partnumber", txtPartNo.Text);
 
Share this answer
 
Comments
CHill60 15-Jan-14 7:17am    
But if @partnumber is an int and the user has typed "banana" into txtPartNo this would still fail.
Karthik_Mahalingam 15-Jan-14 7:27am    
downvoted ???
CHill60 15-Jan-14 9:28am    
(Referring to your deleted comment) My understanding of the problem was that the OP got an error and he didn't want one. I fully understand your solution. I also understand that an exception can still be thrown if the data is not validated first. Griff, Tadit and Sandeep have all give the OP excellent advice which I hope the OP understood. As to the downvoting - there have been 3
Gilbertinino 15-Jan-14 7:20am    
Thx for your answer.
Karthik_Mahalingam 15-Jan-14 7:22am    
welcome :)

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