对象引用未设置为 asp.net 中 sql 命令中的对象的实例

本文关键字:sql 命令 对象 实例 net 设置 asp 对象引用 | 更新日期: 2023-09-27 18:34:24

初始化命令文本时出错。无法在 SQL 命令对象中添加 SQL 更新查询

TextBox id = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtid");
TextBox Loginid = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtgvusername");
TextBox EmployeeId = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtEmployeeId");
TextBox Fullname = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtFullname");
TextBox Password = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtPassword");
TextBox ContactNo = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtContactNo");
TextBox MailId = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtMailid");
TextBox Location = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtLocation");
TextBox Roles = (TextBox)gvUserDetails.Rows[e.RowIndex].FindControl("txtRoles");
CheckBox chkLeads = (CheckBox)gvUserDetails.Rows[e.RowIndex].FindControl("chkLeads");
CheckBox ChkSales = (CheckBox)gvUserDetails.Rows[e.RowIndex].FindControl("ChkSales");
CheckBox ChkReports = (CheckBox)gvUserDetails.Rows[e.RowIndex].FindControl("ChkReports");
CheckBox ChkPayments = (CheckBox)gvUserDetails.Rows[e.RowIndex].FindControl("ChkPayments");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Update tbl_AdminLogin set FullName='" + txtfullName.Text + "',Password='" + txtPassword.Text + "',ContactNo='" + txtContact.Text + "',Mailid='" + txtMailid.Text + "',Location='" + txtLocation.Text + "',Roles='" + Roles.Text + "',Leads='" + chkLeads.Text + "',Sales='" + ChkSales.Checked + "',Reports='" + ChkSales.Text + "',Payments='" + ChkPayments.Text + "',EmployeeId='" + id.Text + "' where id='" + id.Text + "'";//getting error here

对象引用未设置为 asp.net 中 sql 命令中的对象的实例

您尝试查找的其中一个Control找不到,并且FindControl返回null,并且您正在使用该null控件的Text(或Checked)属性。我不知道您的控件的名称是什么,但我怀疑问题出在那一行:

    CheckBox chkLeads = (CheckBox)gvUserDetails.Rows[e.RowIndex].FindControl("chkLeads");

因为您其余CheckBox的名称前缀为"Chk"而不是"chk",请尝试修复它,并查看有关您问题的前两条评论。