2012-07-08 57 views
0

我在sql server中測試我的查詢,它工作在100% 但我的頁面不工作! 這是我的查詢更新不能在更新網格視圖事件中工作

UPDATE Employee SET 
     Name='jojo', 
     Age=19, 
     GenderID=2, 
     CountryID=5, 
     Mobile=0917021092 
WHERE EmployeeID=10 

這是我的代碼

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
     {    
      string s = GridView1.DataKeys[e.RowIndex].Value.ToString(); 

      Label id = (Label)GridView1.Rows[e.RowIndex].FindControl("lblEditID"); 
      Label EmployeeID = (Label)GridView1.Rows[e.RowIndex].FindControl("lblEmployeeID"); 
      TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditName"); 
      TextBox age = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditAge"); 
      DropDownList gender = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropGender"); 
      DropDownList country = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropCountry"); 
      TextBox mobile = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditMobile"); 

      string stringconnectiong = ConfigurationManager.ConnectionStrings["Employee_TestConnectionString"].ConnectionString; 
      string sql = "update Employee set [email protected], [email protected],[email protected] , [email protected] , [email protected] where [email protected] AND [email protected]"; 

      SqlConnection con = new SqlConnection(stringconnectiong); 

      try 
      { 
      con.Open(); 
      SqlCommand cmd = new SqlCommand(); 
      cmd.Parameters.AddWithValue("@EmployeeID", EmployeeID.Text); 
      cmd.Parameters.AddWithValue("@ID", id.Text); 
      cmd.Parameters.AddWithValue("@Name", name.Text); 
      cmd.Parameters.AddWithValue("@Age", age.Text); 
      cmd.Parameters.AddWithValue("@GenderID", gender.SelectedValue); 
      cmd.Parameters.AddWithValue("@CountryID", country.SelectedValue); 
      cmd.Parameters.AddWithValue("@Mobile", mobile.Text); 
      cmd.CommandType = CommandType.Text; 
      cmd.CommandText = sql; 
      cmd.Connection = con; 
      cmd.ExecuteNonQuery(); 
      SqlDataAdapter dr = new SqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      dr.Fill(ds); 
      GridView1.DataSource = ds; 
      GridView1.EditIndex = -1; 
      BindGridview(); 

     } 

     catch (System.Data.SqlClient.SqlException ex) 
     { 
      string msg = "Error Updating "; 
      msg += ex.Message; 
      throw new Exception(msg); 
     } 

     finally 
     { 
      con.Close(); 
      con.Dispose(); 
      BindGridview(); 
     } 

    } 
+0

在您的代碼正常工作之前,請勿添加try catch。看看什麼錯誤消息是第一次生成... – IrishChieftain 2012-07-08 14:18:03

回答

0

由於您的數據源是一個數據集,你需要你通過將DataMember屬性格式使用的數據集內的指定數據表。或者只是使用數據表作爲您的數據源。

有以下替換集線:

DataTable dt = new DataTable(); 
dr.Fill(dt); 
GridView1.DataSource = dt; 

此外,使用GridView1_RowUpdated事件。 GridView1_RowUpdating事件被觸發之前該表已更新,因此您的參數值尚未更新。

+0

請告訴我如何? – 2012-07-10 06:35:49

+0

@MohannedKhaledMaglad,我編輯了我的答案,以展示如何使用數據表而不是數據集。 – 2012-07-10 12:43:53

+0

它也不工作? – 2012-07-10 14:06:26