2014-12-03 47 views
0

我一直在試圖執行一個GridView。在這裏,我的數據庫名稱是test,存儲過程名稱是employee_pro。但是,它不斷顯示相同的錯誤。什麼是必要的解決方案?過程或函數'employee_pro'需要參數'@empid',它沒有提供

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(); 

    } 

} 

}

+1

'employee_pro' sp的定義是什麼?在你的'BindGrid'方法中,你沒有添加'@ empid'作爲參數。 – 2014-12-03 10:08:26

+1

您似乎沒有爲'@ empid'提供值。 – 2014-12-03 10:09:48

+0

錯誤信息是不言自明的! – 2014-12-03 10:11:12

回答

0

在功能Addemployee(),您已添加如下參數:

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

但是您沒有指定參數的值。因此,添加以下行

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

我想你應該添加在BingGrid功能的另一個參數是@empid。提供一個empid以顯示與該empid相關的結果

0

錯誤消息指出您缺少參數。

你應該添加@empidBindGrid()方法:

//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 
+2

請閱讀 - 我們可以停止使用'AddWithValue' - http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ – 2014-12-03 10:16:23

+0

總有改進的餘地。謝謝 – Marco 2014-12-03 10:18:45

0

正如很多人已經指出,需要將員工ID提供給BindGrid()函數。你有一個如何在你的RowUpdating方法中獲得它的例子。

但是,您應該考慮在存儲過程中將其設置爲可選參數。例如,我無法想象在刪除員工時如何爲存儲過程提供emp ID參數。您可能需要對存儲過程的使用進行一些重新設計。

+0

謝謝你們幫助我...它工作.. – Sumi 2014-12-03 10:30:58

相關問題