过程或函数“employee_pro”需要参数“@empid”,但未提供

本文关键字:@empid 参数 函数 employee 过程 pro | 更新日期: 2023-09-27 18:32:58

我一直在尝试执行GridView。在这里,我的数据库名称是测试,存储过程名称是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();
    }
}

}

过程或函数“employee_pro”需要参数“@empid”,但未提供

在函数 Addemployee() 中,您添加了一个参数,如下所示:

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

但您尚未指定参数的值。因此,添加以下行

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

您应该在 BingGrid 函数中添加另一个参数,即 @empid .提供一个 empid 以显示与该 empid<</p>

div class="answers" 相关的结果>

错误消息指出您缺少参数。

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

//assuming your empid parameter is of type int
com.Parameters.Add("@empid", SqlDbType.Int);
com.Parameters["@empid"].Value = valueforEmpId;

或:

cmd.Parameters.Add("@empid", SqlDbType.Int).Value = valueforEmpId;

如果 Empid 应该是空的或空的,请使用

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

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

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