在我的代码的删除选项中,对象引用未设置为对象的实例,并且在我的代码的更新选项中出现另一个错误

本文关键字:选项 我的 代码 更新 另一个 错误 设置 对象引用 删除 对象 实例 | 更新日期: 2023-09-27 18:14:42

我使用了以下代码来删除和编辑gridview中的一些值,但是当我单击删除时,它显示了一个错误:

Object reference not set to an instance of an object. in line 48 i.e. con.open();

请帮我解决这个问题。当我点击编辑并在显示的txtboxes中插入值,然后点击更新,它显示了另一个错误:

Specified argument was out of the range of valid values. in the line 74 of my code i.e.
   EmailID = (TextBox)GridView1.Rows[e.RowIndex].Cells[7].FindControl("EmailID");
我使用的代码是-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Admin_viewSalesmanDetails : System.Web.UI.Page
{
    SqlConnection con;
    String strConnection = "Data Source=HP''SQLEXPRESS;database=MK;Integrated Security=true";
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }

    }
    private void BindGrid()
    {
          using (SqlConnection con = new SqlConnection(strConnection))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select * from salesman_details";
                cmd.Connection = con;

                SqlDataSource1.DataBind();
                GridView1.DataSource = SqlDataSource1;
                GridView1.DataBind();
            }
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label EmpID = new Label();
        EmpID = (Label)GridView1.Rows[e.RowIndex].Cells[2].FindControl("EmpID");
        cmd = new SqlCommand("delete from salesman_setails where EmpID=" + EmpID.Text + "", con);
        con.Open();
        int k = cmd.ExecuteNonQuery();
        con.Close();
        if (k == 1)
        {
            Response.Write("<script>alert('Employee Deleted Successfully')</script>");
            BindGrid();
        }
        else
        {
            Response.Write("<script>alert('Error Occured While Deleting')</script>");
            BindGrid();
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGrid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label EmpID = new Label();
        TextBox PhnNo = new TextBox();
        TextBox EmailID = new TextBox();
        EmpID = (Label)GridView1.Rows[e.RowIndex].Cells[2].FindControl("EmpID");
        PhnNo = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].FindControl("PhnNo");
        EmailID = (TextBox)GridView1.Rows[e.RowIndex].Cells[7].FindControl("EmailID");
        cmd = new SqlCommand("update  salesman_details set PhnNo='" + PhnNo.Text + "', EmailID='" + EmailID.Text + "' where EmpID=" + EmpID.Text + "", con);
        con.Open();
        int k = cmd.ExecuteNonQuery();
        con.Close();
        if (k == 1)
        {
            Response.Write("<script>alert('Employee Updated Successfully')</script>");
            GridView1.EditIndex = -1;
            BindGrid();
        }
        else
        {
            Response.Write("<script>alert('Error Occured While Updating')</script>");
            BindGrid();
        }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGrid();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
}

在我的代码的删除选项中,对象引用未设置为对象的实例,并且在我的代码的更新选项中出现另一个错误

在你的GridView1_RowDeletingGridView1_RowUpdating中,你还没有初始化你的con,你正试图打开它。

可以在声明时实例化连接,也可以在事件使用时实例化连接。

public partial class Admin_viewSalesmanDetails : System.Web.UI.Page
{
      String strConnection = "Data Source=HP''SQLEXPRESS;database=MK;Integrated Security=true";
      SqlConnection con = new SqlConnection(strConnection);
或者更好的方法是在事件中的using语句中实例化它。为例。
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    Label EmpID = new Label();
    EmpID = (Label)GridView1.Rows[e.RowIndex].Cells[2].FindControl("EmpID");
    cmd = new SqlCommand("delete from salesman_setails where EmpID=" + EmpID.Text + "", con);
    using( con = new SqlConnection(strConnection))
    {
        cmd.Connection = con; //here
        con.Open();
        int k = cmd.ExecuteNonQuery();
        con.Close();
        if (k == 1)
        {
            Response.Write("<script>alert('Employee Deleted Successfully')</script>");
            BindGrid();
        }
        else
        {
            Response.Write("<script>alert('Error Occured While Deleting')</script>");
            BindGrid();
        }
    }
}

using语句将确保在连接上调用Dispose(这也将关闭连接),即使发生一些异常。

私有字段在PostBacks之间丢失-在您的事件(GridView1_RowDeleting)中,您正在使用SqlConnection而没有首先初始化它。

添加
con = new SqlConnection(strConnection);

con.Open();

相关文章: