过程或函数'employee_pro'需要参数'@empid',这是未提供的

时间:2014-12-03 10:06:51

标签: 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["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();

    }

}

}

4 个答案:

答案 0 :(得分:0)

我认为你应该在BingGrid函数中添加另一个参数@empid。提供一个empid来显示与该empid相关的结果

答案 1 :(得分:0)

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

您应该在@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应为null或空使用

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

答案 2 :(得分:0)

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

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

但是您没有指定参数的值。因此,添加以下行

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

答案 3 :(得分:0)

正如很多人所说,您需要将员工ID提供给BindGrid()函数。您有一个如何在RowUpdating方法中获取它的示例。

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