locked
add multiple data to database from gridview RRS feed

  • Question

  • User-646447061 posted

    protected void Button1_Click(object sender, EventArgs e)
    {
    string StrQuery;
    try
    {

    using (SqlCommand cmd= new SqlCommand())
    {
    cmd.Connection = con;
    con.Open();
    for(int i=0; i< GridView1.Rows.Count;i++)
    {
    StrQuery= @"INSERT INTO penerimaan_barang VALUES ("
    + GridView1.Rows[1].Cells["id_pb"].Value +", "
    + GridView1.Rows[1].Cells["id_b"].Value +", "
    + GridView1.Rows[1].Cells["tgl_jam"].Value +", "
    + GridView1.Rows[1].Cells["note"].Value +", "
    + GridView1.Rows[1].Cells["lokasi"].Value +", "
    + GridView1.Rows[1].Cells["item"].Value + ", "
    + GridView1.Rows[1].Cells["donatur"].Value +");";
    cmd.CommandText = StrQuery;
    cmd.ExecuteNonQuery();
    }
    con.Close();
    }

    }
    finally{}
    }

    but when i try to run, the code error ..
    1 .The best overloaded method match for 'System.Web.UI.WebControls.TableCellCollection.this[int]' has some invalid arguments 

    2. cannot convert from 'string' to 'int'  ..

    Help me guys

    Sunday, January 24, 2016 4:29 AM

Answers

All replies

  • User1124521738 posted

    it's entirely likely that there are quotes or comas in your content.  you are concatenating values rather than using parameters so the values you are passing in are part of the query text rather than being seen as values to insert.

    see http://www.csharp-station.com/Tutorial/AdoDotNet/Lesson06 and https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters%28v=vs.110%29.aspx for examples on using parameters with ado.net

    also, you should specify the insert columns rather than rely on the definition order - if you re-order or add additional columns it may quietly break.

    you're also using GridView1.Rows[1] rather than GridView1.Rows[i] so you're going to get however many rows in the grid copies of the second row (c# indexes start with 0).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 24, 2016 4:44 AM
  • User-646447061 posted

    you're also using GridView1.Rows[1] rather than GridView1.Rows[i] so you're going to get however many rows in the grid copies of the second row (c# indexes start with 0).

    i try to changes 1 to i but same error ..

    Sunday, January 24, 2016 6:31 AM
  • User-646447061 posted

    i try to changes code like this ..

    protected void Button1_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
    string StrQuery = @"INSERT INTO penerimaan_barang VALUES ('" + GridView1.Rows[i].Cells["id_pb"].Value + "', '" + GridView1.Rows[i].Cells["tgl_jam"].Value + "',' " + GridView1.Rows[i].Cells["note"].Value + "',' " + GridView1.Rows[i].Cells["lokasi"].Value + "', '" + GridView1.Rows[i].Cells["item"].Value + "', '" + GridView1.Rows[i].Cells["donatur"].Value + "');";

    try
    {
    SqlConnection conn = new SqlConnection();
    con.Open();

    using (SqlCommand cmd = new SqlCommand(StrQuery, con))
    {
    cmd.ExecuteNonQuery();
    }
    con.Close();

    }
    }
    }

    and i get error : excepted catch or finally

    Sunday, January 24, 2016 6:37 AM
  • User-219423983 posted

    Hi motorrevo00,

    You should first pass the connection string to your SqlConnection “conn”. Then, you could get the “StrQuery” and then check whether it has the expected value when you debug your code. You could also execute the SQL string in your database to check whether it could work normally.

    Besides, you could have a look at the following link which provide some demos about ADO.NET. As suggested ninianne98 above, you’d better use the SQL command parameters to implement your needs.

    https://msdn.microsoft.com/en-us/library/dw70f090(v=vs.110).aspx

    Best Regards,

    Weibo Zhang

    Sunday, January 24, 2016 7:01 AM
  • User465171450 posted

    This isn't a sql server issue. This has to do with how you are accessing the gridview cells. You are attempting to use a string to find them, that won't work. You must include the ordinal position of the cell. Hence the string to int conversion error.

    Sunday, January 24, 2016 10:09 PM
  • User-1716253493 posted

    motorrevo00

    1 .The best overloaded method match for 'System.Web.UI.WebControls.TableCellCollection.this[int]' has some invalid arguments 

    2. cannot convert from 'string' to 'int'  ..

    Help me guys

    Cell don't have cell name (string), use int as cellindex Cells["id_pb"] => Cells[0]

    GridView1.Rows[i].Cells[0].Text

    Use i intead of 1 Rows[1] => Rows[i]

    Use Text property instead of Value Cells[0].Value => Cells[0].Text

    Monday, January 25, 2016 1:01 AM