Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 过程或函数“employee_pro”需要未提供的参数“@empid”_C#_Asp.net_Gridview - Fatal编程技术网

C# 过程或函数“employee_pro”需要未提供的参数“@empid”

C# 过程或函数“employee_pro”需要未提供的参数“@empid”,c#,asp.net,gridview,C#,Asp.net,Gridview,我一直在尝试执行GridView。这里,我的数据库名是test,存储过程名是employee_pro。但是,它不断地显示出同样的错误。必要的解决办法是什么 namespace Insert_update_delete_Stored_Pro { public partial class StoredProcedure : System.Web.UI.Page { string strConnString = ConfigurationManager.ConnectionStrings[

我一直在尝试执行GridView。这里,我的数据库名是test,存储过程名是employee_pro。但是,它不断地显示出同样的错误。必要的解决办法是什么

namespace Insert_update_delete_Stored_Pro
{

public partial class StoredProcedure : System.Web.UI.Page
{

    string strConnString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;

    SqlCommand com;
    SqlDataAdapter sqlda;
    DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    protected void BindGrid()
    {
        SqlConnection con = new SqlConnection(strConnString);

        com = new SqlCommand();
        con.Open();
        com.Connection = con;
        com.CommandText = "employee_pro";
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50));                           
        com.Parameters["@status"].Value = "Display";
        sqlda = new SqlDataAdapter(com);
        ds = new DataSet();
        sqlda.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }



    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        GridView1.PageIndex = e.NewPageIndex;

        BindGrid();

    }



    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

        GridView1.EditIndex = -1;

        BindGrid();

    }



    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("Add"))
        {

            TextBox txtname = (TextBox)GridView1.FooterRow.FindControl("txtAddname");

            TextBox txtaddress = (TextBox)GridView1.FooterRow.FindControl("txtAddaddress");

            TextBox txtdesignation = (TextBox)GridView1.FooterRow.FindControl("txtAdddesignation");

            string name, address, designation;

            name = txtname.Text;

            address = txtaddress.Text;

            designation = txtdesignation.Text;

            Addemployee(name, address, designation);

            GridView1.EditIndex = -1;

            BindGrid();

        }

    }

    protected void Addemployee(string name, string address, string designation)
    {

        SqlConnection con = new SqlConnection(strConnString);
        con.Open();
        com = new SqlCommand();
        com.CommandText = "employee_pro"; 
        com.CommandType = CommandType.StoredProcedure; 
        com.Connection = con; 
        com.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50));
        com.Parameters.Add(new SqlParameter("@empid", SqlDbType.Int)); 
        com.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar, 50)); 
        com.Parameters.Add(new SqlParameter("@address", SqlDbType.VarChar, 50)); 
        com.Parameters.Add(new SqlParameter("@designation", SqlDbType.VarChar, 50)); 
        com.Parameters["@status"].Value = "Add"; 
        com.Parameters["@name"].Value = name; 
        com.Parameters["@address"].Value = address; 
        com.Parameters["@designation"].Value = designation; 
        sqlda = new SqlDataAdapter(com); 
        ds = new DataSet(); 
        sqlda.Fill(ds); 
        con.Close(); 
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        Label empid = (Label)GridView1.Rows[e.RowIndex].FindControl("lblempid");

        string eid = empid.Text;

        Deleteemployee(eid);

        GridView1.EditIndex = -1;

        BindGrid();

    }

    protected void Deleteemployee(string empid)
    {

        SqlConnection con = new SqlConnection(strConnString);

        com = new SqlCommand();

        com.CommandText = "employee_pro";

        com.CommandType = CommandType.StoredProcedure;

        com.Connection = con;

        com.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50));

        com.Parameters.Add(new SqlParameter("@empid", SqlDbType.Int));

        com.Parameters["@status"].Value = "Delete";

        com.Parameters["@empid"].Value = empid;

        sqlda = new SqlDataAdapter(com);

        ds = new DataSet();

        sqlda.Fill(ds);

        con.Close();

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView1.EditIndex = e.NewEditIndex;

        BindGrid();

    }



    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        Label empid = (Label)GridView1.Rows[e.RowIndex].FindControl("lblempid");

        TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname");

        TextBox address = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtaddress");

        TextBox designation = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdesignation");

        string eid = empid.Text;

        string ename = name.Text;

        string eaddress = address.Text;

        string edesignation = designation.Text;

        Updateemployee(eid, ename, eaddress, edesignation);

        GridView1.EditIndex = -1;

        BindGrid();

    }



    protected void Updateemployee(string empid, string name, string address, string designation)
    {

        SqlConnection con = new SqlConnection(strConnString);

        com = new SqlCommand();

        con.Open();

        com.Connection = con;

        com.CommandText = "employee_pro";

        com.CommandType = CommandType.StoredProcedure;

        com.Parameters.Add(new SqlParameter("@empid", SqlDbType.Int));

        com.Parameters.Add(new SqlParameter("@status", SqlDbType.VarChar, 50));

        com.Parameters.Add(new SqlParameter("@name", SqlDbType.VarChar, 50));

        com.Parameters.Add(new SqlParameter("@address", SqlDbType.VarChar, 50));

        com.Parameters.Add(new SqlParameter("@designation", SqlDbType.VarChar, 50));

        com.Parameters["@empid"].Value = Convert.ToInt32(empid.ToString());

        com.Parameters["@status"].Value = "Update";

        com.Parameters["@name"].Value = name;

        com.Parameters["@address"].Value = address;

        com.Parameters["@designation"].Value = designation;

        sqlda = new SqlDataAdapter(com);

        ds = new DataSet();

        sqlda.Fill(ds);

        con.Close();

    }

}

}

我认为应该在BingGrid函数中添加另一个参数@empid。提供一个empid以显示与该empid相关的结果。错误消息表明您缺少一个参数

您应该将@empid添加到BindGrid方法中:

或:

如果Empid应该为null或空,请使用

 com.Parameters["@empid"].Value = DBNull.Value
在函数Addemployee中,您添加了一个参数,如下所示:

com.Parameters.Add(new SqlParameter("@empid", SqlDbType.Int)); 
但您尚未指定参数的值。因此,添加以下行

com.Parameters["@empid"].Value = <Generate Your Employee Id Here>; 

正如很多人已经说过的,您需要向BindGrid函数提供雇员ID。您有一个如何在RowUpdate方法中获得它的示例


但是,您应该考虑将其作为存储过程中的可选参数。例如,在删除员工时,我无法想象您需要如何向存储过程提供emp ID参数。您可能需要围绕存储过程的使用进行一些重新设计。

employee\u pro sp的定义是什么?在BindGrid方法中,您没有添加@empid作为参数。您似乎没有为@empid提供值。错误消息是不言自明的@Sumi,你能提供一个包含错误的堆栈跟踪吗?哪种方法失败?没有为BindGrid方法中的employee_pro过程提供@empid输入参数。请检查。请阅读-我们可以停止使用AddWithValue吗-总有改进的余地。非常感谢。
com.Parameters.Add(new SqlParameter("@empid", SqlDbType.Int)); 
com.Parameters["@empid"].Value = <Generate Your Employee Id Here>;