Click here to Skip to main content
13,295,953 members (52,639 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
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 10-Sep-17 18:34pm
Updated 10-Sep-17 19:28pm
v2
Comments
PIEBALDconsult 10-Sep-17 23:59pm
   
That way is bad. The right way is good.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
Comments
H M Mori 11-Sep-17 2:10am
   
sorry but not it is not asp.net question.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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())
  Permalink  
v2
Comments
H M Mori 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171207.1 | Last Updated 11 Sep 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100