2017-08-01 57 views
0

我,當我點擊編輯按鈕發送值與其它頁面:如何獲得價值和更新3個表ASP.NET

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
    { 
     if (e.CommandName == "EditButton") 
     { 
      string[] arg = new string[3]; 
      arg = e.CommandArgument.ToString().Split(';'); 
      Session["EmployeeID"] = arg[0]; 
      Session["ItemID"] = arg[1]; 
      Session["InventoryID"] = arg[2]; 
      //int index = Convert.ToInt32(e.CommandArgument); 
      //GridViewRow row = GridView1.Rows[index]; 
      Response.Redirect("~/EditLaptop.aspx?EmployeeID=" + Session["EmployeeID"].ToString().Trim() + "&ItemID=" + Session["ItemID"].ToString().Trim() 
       + "&InventoryID" + Session["InventoryID"].ToString().Trim()); 


     } 
    } 

得到第一頁值:

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

     string empno; 
     string itemid; 
     string inventoryid; 


     protected void Page_Load(object sender, EventArgs e) 
     { 

      empno = Request.QueryString["EmployeeID"].ToString(); 
      itemid = Request.QueryString["ItemID"].ToString(); 
      inventoryid = Request.QueryString["InventoryID"].ToString(); 

當我點擊更新按鈕它顯示錯誤。

protected void OnUpdate(object sender, EventArgs e) 
{ 
    string StrConnString = ConfigurationManager.ConnectionStrings["BenzConnectionString"].ToString(); 
    SqlConnection objConn = new SqlConnection(StrConnString); 

    try 
    { 

     objConn.Open(); 
     SqlCommand objCmd = new SqlCommand("UPDATE Employee SET FirstName = @sFirstName," + 
      " LastName = @sLastName," + 
      " EMail = @sEMail, " + 
      "PhoneNo = @sPhoneNo, " + 
      "PositionID = @sPositionID, " + 
      "DepartmentID = @sDepartmentID" + 
      " WHERE EmployeeID = @sEmployeeID", objConn); 

     objCmd.Parameters.AddWithValue("@sFirstName", txtfirstname.Text); 
     objCmd.Parameters.AddWithValue("@sLastName", txtlastname.Text); 
     objCmd.Parameters.AddWithValue("@sEMail", txtemail.Text); 
     objCmd.Parameters.AddWithValue("@sPhoneNo", txtphone.Text); 
     objCmd.Parameters.AddWithValue("@sPositionID", dpposition.Text); 
     objCmd.Parameters.AddWithValue("@sDepartmentID", dpcenter.Text); 
     objCmd.Parameters.AddWithValue("@sEmployeeID", empno); 

     objCmd.ExecuteNonQuery(); 

     objCmd = new SqlCommand("UPDATE Item SET Brand = @sBrand, " + 
      "Model = @sModel, " + 
      "Serial_No = @sSerial_No, " + 
      "Macaddress = @sMacaddress, " + 
      "ItemTypeID = @sItemTypeID, " + 
      "ReceiveDate = @sReceiveDate " + 
      "WHERE ItemID = @sItemID ", objConn); 

     objCmd.Parameters.AddWithValue("@sBrand", txtbrand.Text); 
     objCmd.Parameters.AddWithValue("@sModel", txtmodel.Text); 
     objCmd.Parameters.AddWithValue("@sSerial_No", txtserial.Text); 
     objCmd.Parameters.AddWithValue("@sMacaddress", txtmac.Text); 
     objCmd.Parameters.AddWithValue("@sItemTypeID", dptype.Text); 
     objCmd.Parameters.AddWithValue("@sReceiveDate", txtreceiveddate.Text); 
     objCmd.Parameters.AddWithValue("@sItemID", itemid); 


     objCmd = new SqlCommand("UPDATE SET InventoryLine SET Transaction_date = @sTransaction_date," + 
      " ReturnDate = @sReturnDate " + 
      "WHERE InventoryID = @sInventoryID", objConn); 
     objCmd.Parameters.AddWithValue("@sReturnDate", txtreturndate.Text); 
     objCmd.Parameters.AddWithValue("@sTransaction_date", txtreturndate.Text); 
     objCmd.Parameters.AddWithValue("@sInventoryID", inventoryid); 
     objCmd.ExecuteNonQuery(); 
    } 
    catch (Exception ex) 
    { 
     Response.Write("<br/> Error : " + ex.Message); 
    } 
    finally 
    { 
     objConn.Close(); 
    } 
} 
+2

請告訴我你的錯誤越來越?那麼什麼顯示你的異常消息? –

+0

你應該看看[我們可以停止使用AddWithValue()了嗎?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/)並停止使用'.AddWithValue()' - 它可能會導致意想不到的和令人驚訝的結果... –

+0

嗨丹尼斯請參閱第二個答案上的錯誤消息 –

回答

0

使用這樣的代碼。

protected void OnUpdate(object sender, EventArgs e) 
{ 
    string StrConnString = ConfigurationManager.ConnectionStrings["BenzConnectionString"].ToString(); 
    SqlConnection objConn = new SqlConnection(StrConnString); 

    try 
    { 

     objConn.Open(); 

     using(SqlCommand objCmd1 = new SqlCommand("UPDATE Employee SET FirstName = @sFirstName," + 
      " LastName = @sLastName," + 
      " EMail = @sEMail, " + 
      "PhoneNo = @sPhoneNo, " + 
      "PositionID = @sPositionID, " + 
      "DepartmentID = @sDepartmentID" + 
      " WHERE EmployeeID = @sEmployeeID", objConn){ 

     objCmd.Parameters.AddWithValue("@sFirstName", txtfirstname.Text); 
     objCmd.Parameters.AddWithValue("@sLastName", txtlastname.Text); 
     objCmd.Parameters.AddWithValue("@sEMail", txtemail.Text); 
     objCmd.Parameters.AddWithValue("@sPhoneNo", txtphone.Text); 
     objCmd.Parameters.AddWithValue("@sPositionID", dpposition.Text); 
     objCmd.Parameters.AddWithValue("@sDepartmentID", dpcenter.Text); 
     objCmd.Parameters.AddWithValue("@sEmployeeID", empno); 

     objCmd.ExecuteNonQuery(); 
} 

     using(SqlCommand objCmd2 = new SqlCommand("UPDATE Item SET Brand = @sBrand, " + 
      "Model = @sModel, " + 
      "Serial_No = @sSerial_No, " + 
      "Macaddress = @sMacaddress, " + 
      "ItemTypeID = @sItemTypeID, " + 
      "ReceiveDate = @sReceiveDate " + 
      "WHERE ItemID = @sItemID ", objConn){ 

     objCmd.Parameters.AddWithValue("@sBrand", txtbrand.Text); 
     objCmd.Parameters.AddWithValue("@sModel", txtmodel.Text); 
     objCmd.Parameters.AddWithValue("@sSerial_No", txtserial.Text); 
     objCmd.Parameters.AddWithValue("@sMacaddress", txtmac.Text); 
     objCmd.Parameters.AddWithValue("@sItemTypeID", dptype.Text); 
     objCmd.Parameters.AddWithValue("@sReceiveDate", txtreceiveddate.Text); 
     objCmd.Parameters.AddWithValue("@sItemID", itemid); 


     objCmd = new SqlCommand("UPDATE SET InventoryLine SET Transaction_date = @sTransaction_date," + 
      " ReturnDate = @sReturnDate " + 
      "WHERE InventoryID = @sInventoryID", objConn); 
     objCmd.Parameters.AddWithValue("@sReturnDate", txtreturndate.Text); 
     objCmd.Parameters.AddWithValue("@sTransaction_date", txtreturndate.Text); 
     objCmd.Parameters.AddWithValue("@sInventoryID", inventoryid); 
     objCmd.ExecuteNonQuery(); 
} 
    } 
    catch (Exception ex) 
    { 
     Response.Write("<br/> Error : " + ex.Message); 
    } 
    finally 
    { 
     objConn.Close(); 
    } 
} 
0

有錯誤信息,當我點擊更新

Server Error in '/' Application. 

Object reference not set to an instance of an object. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. 

Source Error: 


Line 24:   empno = Request.QueryString["EmployeeID"].ToString(); 
Line 25:   itemid = Request.QueryString["ItemID"].ToString(); 
Line 26:   inventoryid = Request.QueryString["InventoryID"].ToString(); 
Line 27: 
Line 28: