【问题标题】:delete records using gridview ASP.NET使用 gridview ASP.NET 删除记录
【发布时间】:2017-03-21 20:21:41
【问题描述】:

我写信是为了寻求有关使用复选框 gridview 控件删除数据库记录的帮助。

我正在尝试使用复选框和网格视图从“ViewData”中删除记录,但“if”条件一直失败。任何关于我可能出错的地方的进一步帮助将不胜感激。非常感谢您的宝贵时间和反馈。

//Method for Deleting Record  
protected void DeleteRecord(int ID)
{
    SqlConnection con = new SqlConnection(cs);
    SqlCommand com = new SqlCommand("delete from tbl_Users where ID=@ID", con);
    com.Parameters.AddWithValue("@ID", ID);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
}

protected void btnDeleteRecord_Click(object sender, EventArgs e)
{
    foreach (GridViewRow grow in ViewDataGrid.Rows)
    {
//Searching CheckBox("chkDel") in an individual row of Grid  
CheckBox chkdel = (CheckBox)grow.FindControl("chkDel");
//If CheckBox is checked than delete the record with particular empid  
if (chkdel.Checked)
{
    int ID = Convert.ToInt32(grow.Cells[1].Text);
    DeleteRecord(ID);
}
    }
    //Displaying the Data in GridView  
    showData();
}

错误:输入字符串的格式不正确: int ID = Convert.ToInt32(grow.Cells[1].Text);

【问题讨论】:

  • grow.Cells[1] 中的任何内容都无法转换为 int。那个牢房里有什么?
  • 什么是grow.Cells[1].Text?
  • 根据您的问题对我帮助很大,我没有将ID单元格放在表格中!怜悯 :) 非常感谢! :)

标签: c# asp.net gridview


【解决方案1】:

在下一行

int ID = Convert.ToInt32(grow.Cells[1].Text);

grow.Cells[1].Text 的实际值可能包含字母字符或空格,因此您会收到此错误。

Convert.ToInt32(stringVal) 实际上将数字的指定字符串表示形式转换为等效的 32 位有符号整数,但参数应该是仅包含数字的字符串。

【讨论】:

    【解决方案2】:

    在您的选择查询中 字符串 SelectQuery = "SELECT FROM tbl_Users WHERE ID=" + ID; 您缺少您选择的值。

    您必须选择输入 * 所需的特定列才能选择所有列。 例如字符串 SelectQuery = "SELECT * FROM tbl_Users WHERE ID=" + ID; 或者 string SelectQuery = "SELECT column1,column3 FROM tbl_Users WHERE ID=" + ID;

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2018-12-07
      • 2016-08-18
      • 2014-10-21
      • 2012-04-27
      • 2011-11-15
      • 1970-01-01
      相关资源
      最近更新 更多