Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Frnds,

C#
private void button3_Click(object sender, EventArgs e)
{                    
   try
   {
      for (int i = 0; i < dataGridView1.Rows.Count-1 ; i++)
      {
         BILLId();
         ID();
         SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=ParamBills;Persist Security Info=True;User ID=sa;Password=login");
         SqlCommand cmd = new SqlCommand("insert_Sales_Details", con);
         con.Open();

         cmd.CommandType = CommandType.StoredProcedure;
         cmd.Parameters.Add("@id", SqlDbType.Int ).Value = textBox9.Text;
         cmd.Parameters.Add("@Bill_No", SqlDbType.NVarChar).Value = textBox8.Text;
         cmd.Parameters.Add("@SDate", SqlDbType.DateTime).Value = dateTimePicker1.Text;
         cmd.Parameters.Add("@Sales_Person", SqlDbType.NVarChar).Value = textBox6.Text;
         cmd.Parameters.Add("@Cashcounter_No", SqlDbType.NVarChar).Value = textBox7.Text;
         cmd.Parameters.Add("@Tax2", SqlDbType.Float).Value = textBox3.Text;
         cmd.Parameters.Add("@Sub_Total", SqlDbType.Float).Value = textBox10.Text;
         cmd.Parameters.Add("@Discount", SqlDbType.Float ).Value = textBox5.Text;
         cmd.Parameters.Add("@Total", SqlDbType.Float).Value = textBox2.Text;
         cmd.Parameters.Add("@Total2", SqlDbType.Float).Value = textBox4.Text;
         cmd.Parameters.Add("Total_Item", SqlDbType.Int).Value = textBox11.Text;
                              
         cmd.Parameters.Add("@Product_Code", SqlDbType.NVarChar).Value = dataGridView2.Rows[i].Cells[0].Value;
         cmd.Parameters.Add("@Product_Type", SqlDbType.NVarChar).Value = dataGridView2.Rows[i].Cells[1].Value;
         cmd.Parameters.Add("@Product_Type_Code", SqlDbType.NVarChar).Value = dataGridView2.Rows[i].Cells[2].Value;
         cmd.Parameters.Add("@Product_Name", SqlDbType.NVarChar).Value = dataGridView2.Rows[i].Cells[3].Value;
         cmd.Parameters.Add("@Item_Code", SqlDbType.NVarChar).Value = dataGridView2.Rows[i].Cells[4].Value;
         // cmd.Parameters.AddWithValue("@Item_Code", dataGridView2.Rows[i].Cells[4].Value);
         cmd.Parameters.Add("@Item_Name", SqlDbType.NVarChar).Value = dataGridView2.Rows[i].Cells[5].Value;
         cmd.Parameters.Add("@Qty", SqlDbType.Int).Value = dataGridView2.Rows[i].Cells[6].Value;
         cmd.Parameters.Add("@Tax", SqlDbType.Float).Value = dataGridView2.Rows[i].Cells[7].Value;
         cmd.Parameters.Add("@Selling_Price", SqlDbType.Float).Value = dataGridView2.Rows[i].Cells[8].Value;
         cmd.Parameters.Add("@Product_Price", SqlDbType.Float).Value = dataGridView2.Rows[i].Cells[9].Value;
                    
         cmd.ExecuteNonQuery();
         con.Close();
      }
      MessageBox.Show("Saved");
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}
Posted
Updated 21-Dec-13 0:03am
v3

HI its a typing error..


Item_Code -> change it to Item_code

in sql it is defined as "Item_code" but in c# you have typed as "Item_Code"
 
Share this answer
 
v3
Use the debugger: First break out the line
C#
cmd.Parameters.Add("@Item_Code", SqlDbType.NVarChar).Value = dataGridView2.Rows[i].Cells[4].Value;
To
C#
object obj = dataGridView2.Rows[i].Cells[4].Value;
cmd.Parameters.Add("@Item_Code", SqlDbType.NVarChar).Value = obj;
Then put a breakpoint on the the second line of those two, and examine the contents of "obj"
Probably, it is null - which is confusing SQL unless you said the parameter could be null in your stored procedure.
 
Share this answer
 
Comments
Vivek.anand34 21-Dec-13 6:53am    
Now too same error..
OriginalGriff 21-Dec-13 7:05am    
I didn't expect it to cure the problem: the idea is to get information on what is causing it. So what is the value?
Vivek.anand34 21-Dec-13 7:14am    
Value is ITEM001
OriginalGriff 21-Dec-13 7:21am    
That is every time round the loop, or just the first one? You need the value when the operation fails...
check out this line a proper through brake point....
C#
cmd.Parameters.Add("@Item_Code", SqlDbType.NVarChar).Value = dataGridView2.Rows[i].Cells[4].Value;


you can try this like a hole the value of another string data type.

C#
string Testing = dataGridView2.Rows[i].Cells[4].Value;


if Testing have a some value then your program have another error..check out with the help of Break point.
 
Share this answer
 
check your stored procedure whether you have supplied and check your application @Item_code supplied or not.
 
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