1

I got the excel value in gridview and now I need to insert all the values in rows to sql server 2008.

When i try to iterate throught Gridview rows it throws the error in for loop near the dg_AgentSFR.Rows as "DataGrid' does not contain a definition for 'Rows' "

Here is my code:

protected void savedatafromgv()
{
    foreach (GridViewRow g1 in ***dg_AgentSFR.Rows)***
    {

        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = con.CreateCommand();
        cmd = new SqlCommand("INSERT INTO TB_TransAgenSeaFreightRate(POL,POD,FORWARDER,FORWARDER REFERENCE,SHIPPING LINE,CONTAINER TYPE,CONTAINER SIZE,VALIDITY FROM,VALIDITY TO,BASIC RATE,PAF,CAF,PSS,TOTAL AMOUNT,REE DAYS,CREDIT DAYS,NIT DEPOSIT,COMPANYID,ISACTIVE) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "','" + g1.Cells[7].Text + "','" + g1.Cells[8].Text + "','" + g1.Cells[9].Text + "','" + g1.Cells[10].Text + "','" + g1.Cells[11].Text + "','" + g1.Cells[12].Text + "','" + g1.Cells[13].Text + "','" + g1.Cells[14].Text + "','" + g1.Cells[15].Text + "','" + g1.Cells[16].Text + "',1,'" + TXTCompanyID.Text + "')", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

    Response.Write ("Records inserted successfully");
}

Please help me to resolve this.

Thanks in advance.

Abbas
  • 14,186
  • 6
  • 41
  • 72
Appdev
  • 189
  • 2
  • 6
  • 28

4 Answers4

1

Datagrid does not contain a definition for rows. Instead of rows, it has items.

use this

foreach (DataGridItem Dr in dg_AgentSFR.items)

DataGrid Class

And also use parameterized query to avoid How does SQLParameter prevent SQL Injection

cmd = new SqlCommand("INSERT INTO TB_TransAgenSeaFreightRate(POL,POD,FORWARDER....) values (@POL,@POD,@FORWARDER)
Community
  • 1
  • 1
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
0

try this code

if(dg_AgentSFR.Rows.Count>0)
{
  foreach (GridViewRow g1 in dg_AgentSFR.Rows)
  {

     SqlConnection con = new SqlConnection(strConnString);
     SqlCommand cmd = con.CreateCommand();
     cmd = new SqlCommand("INSERT INTO TB_TransAgenSeaFreightRate(POL,POD,FORWARDER,FORWARDER REFERENCE,SHIPPING LINE,CONTAINER TYPE,CONTAINER SIZE,VALIDITY FROM,VALIDITY TO,BASIC RATE,PAF,CAF,PSS,TOTAL AMOUNT,REE DAYS,CREDIT DAYS,NIT DEPOSIT,COMPANYID,ISACTIVE) values ('" + g1.Cells[0].Text + "','" + g1.Cells[1].Text + "','" + g1.Cells[2].Text + "','" + g1.Cells[3].Text + "','" + g1.Cells[4].Text + "','" + g1.Cells[5].Text + "','" + g1.Cells[6].Text + "','" + g1.Cells[7].Text + "','" + g1.Cells[8].Text + "','" + g1.Cells[9].Text + "','" + g1.Cells[10].Text + "','" + g1.Cells[11].Text + "','" + g1.Cells[12].Text + "','" + g1.Cells[13].Text + "','" + g1.Cells[14].Text + "','" + g1.Cells[15].Text + "','" + g1.Cells[16].Text + "',1,'" + TXTCompanyID.Text + "')", con);
     con.Open();
     cmd.ExecuteNonQuery();
     con.Close();
  }

  Response.Write ("Records inserted successfully");
}
Aftab Ahmed
  • 1,727
  • 11
  • 15
  • Hi Thanks for Quick replay actually i'm not getting "Rows" in the gridview property list.When i keep .(period operator) in the pop up list there is no Rows. <"dg_AgentSFR.Rows"> – Appdev Mar 24 '14 at 09:55
0

A datagrid in ASP.NET does indeed not contain a property Rows. The GridView on the other hand, does contain a property Rows. More info:

I suggest you use the GridView, this is kind of the successor of the DataGrid. And another important tip: use SQL parameters and not just a string-query (SQL injection).

Abbas
  • 14,186
  • 6
  • 41
  • 72
0

Make sure you use GridViewRowEventArgs and NOT GridViewCommandEventArgs

protected void gvSample_RowDataBound(object sender, GridViewRowEventArgs e)

{

//Your code here

}