Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
if (value > 0)
{
    for(int i=0;i<mydatagrid.Rows.Count;i++)
    {
            SqlCommand cc = new SqlCommand("insert into store values(@sbno,@vno,@box,@qty,@pcs,@remark,@avail)", cn);
        cc.Parameters.AddWithValue("@sbno", sbno);
        cc.Parameters.AddWithValue("@vno", vno);
        cc.Parameters.AddWithValue("@box",Int32.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString()));
        cc.Parameters.AddWithValue("@qty",Int32.Parse(dataGridView1.Rows[i].Cells[1].Value.ToString()));
        cc.Parameters.AddWithValue("@pcs",);
        cc.Parameters.AddWithValue("@remark",);
        cc.Parameters.AddWithValue("@avail", );
        cc.ExecuteNonQuery();
    }
   MessageBox.Show(nstdbox + " Boxes Stored To Store");
}


What I have tried:

it is bad or ok? I don't know the practice about this
Am i doing it wrong way?
Posted
Updated 10-Sep-17 18:28pm
v2
Comments
PIEBALDconsult 10-Sep-17 23:59pm    
That way is bad. The right way is good.

How about using a Stored Procedure and pass a DataTable (Grid rows) as parameter and do the Insert/Update in the stored procedure? Here is an example that could be useful.

Sending a DataTable to a Stored Procedure[^]
 
Share this answer
 
Comments
[no name] 11-Sep-17 2:10am    
sorry but not it is not asp.net question.
The basic technique for this is:

Set up the command
Add the parameters -- do not use AddWithValue
Iterate the rows
  Set the parameter values
  Execute the command


And do not do anything like: Int32.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString())
 
Share this answer
 
v2
Comments
[no name] 11-Sep-17 2:12am    
And do not do anything like: Int32.Parse(dataGridView1.Rows[i].Cells[0].Value.ToString())

i have to do this because i want integer value is there any other way to get it?
PIEBALDconsult 11-Sep-17 8:42am    
Of course; just let the system take care of it for you.
David_Wimbley 12-Sep-17 12:09pm    
I think OP's problem is that you're vaguely telling him the route to go and also telling him what not to do but not offering up what he should do. "just let the system take care of it for you" isn't very helpful.

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