System.Web.UI.WebControls.DataGrid& # 39;不包含'行'的定义

本文关键字:包含 定义 UI Web WebControls DataGrid System | 更新日期: 2023-09-27 17:52:16

我得到了gridview中的excel值,现在我需要将行中的所有值插入sql server 2008。

当我尝试遍历Gridview行时,它会在dg_AgentSFR附近的for循环中抛出错误。作为"DataGrid "的行不包含'Rows'的定义"

下面是我的代码:

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");
}

请帮我解决这个问题。

System.Web.UI.WebControls.DataGrid& # 39;不包含'行'的定义

Datagrid不包含rows的定义。用items代替rows

使用这个

foreach (DataGridItem Dr in dg_AgentSFR.items)

DataGrid类SQLParameter如何防止SQL Injection

cmd = new SqlCommand("INSERT INTO TB_TransAgenSeaFreightRate(POL,POD,FORWARDER....) values (@POL,@POD,@FORWARDER)

试试这个代码

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");
}

ASP中的数据网格。. NET确实不包含Rows属性。另一方面,GridView确实包含一个属性Rows。更多信息:

    <
  • DataGrid类/strong>
  • GridView类

我建议你使用GridView,这是DataGrid的继承者。另一个重要提示:使用SQL参数,而不仅仅是字符串查询(SQL注入)。

确保使用GridViewRowEventArgs而不是GridViewCommandEventArgs

protected void gvSample_RowDataBound(object sender, GridViewRowEventArgs e)

{

//你的代码在这里

}