我一直在試圖執行一個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();
}
}
}
'employee_pro' sp的定義是什麼?在你的'BindGrid'方法中,你沒有添加'@ empid'作爲參數。 – 2014-12-03 10:08:26
您似乎沒有爲'@ empid'提供值。 – 2014-12-03 10:09:48
錯誤信息是不言自明的! – 2014-12-03 10:11:12