2011-03-31 220 views
0

我有一個web應用程序,當一個頁面加載時,地址細節將從數據庫中提取出來並顯示在相應的文本字段中。但是,當我嘗試更新和保存數據時,數據不會更新。數據庫更新

但是,當通過點擊一個按鈕來提取數據時,同樣的工作正常。

下面的代碼:

public partial class Address : System.Web.UI.Page 
{ 
    string global; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      global = Session["ID"].ToString(); 

      System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Server = INLD50045747A\\SQLEXPRESS; Database = MyDatabase;User ID = sa; Password = Welcome1; Trusted_Connection = False;"); 
      //SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\MyDatabase.mdf;Integrated Security=True;User Instance=True"); 
      con.Open(); 


      SqlCommand cmd = new SqlCommand("SELECT PermanentAdd,PermanentAdd2, HomePlace, HomeState, HomePin FROM EMPLOYEE_FULLADDRESS_TABLE WHERE EmployeeID = '" + global + "'", con); 

      SqlDataReader x = cmd.ExecuteReader(); 
      while (x.Read()) 
      { 
       TextBox1.Text = (string)x["PermanentAdd"]; 
       TextBox1.Enabled = false; 

       TextBox5.Text = (string)x["PermanentAdd2"]; 
       TextBox5.Enabled = false; 

       TextBox2.Text = (string)x["HomePlace"]; 
       TextBox2.Enabled = false; 

       TextBox3.Text = (string)x["HomeState"]; 
       TextBox3.Enabled = false; 

       State.Items.FindByText(State.SelectedItem.Text).Selected = false; 
       State.Items.FindByText(TextBox3.Text).Selected = true; 
       State.Enabled = false; 

       TextBox4.Text = (string)x["HomePin"]; 
       TextBox4.Enabled = false; 
      } 
      x.Close(); 
      con.Close(); 
     } 


    } 

    protected void UpdateButton_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Server = INLD50045747A\\SQLEXPRESS; Database = MyDatabase;User ID = sa; Password = Welcome1; Trusted_Connection = False;"); 
      //System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\MyDatabase.mdf;Integrated Security=True;User Instance=True"); 
      con.Open(); 
      // global = Session["ID"].ToString(); 


      //string insert = "UPDATE EMPLOYEE_FULLADDRESS_TABLE SET PermanentAdd = @PermanentAdd, PermanentAdd2 = @PermanentAdd2, HomePlace = @HomePlace, HomeState= @HomeState, HomePin= @HomePin where EmployeeID = '" + global + "'"; 
      SqlCommand cmd1 = new SqlCommand("UPDATE EMPLOYEE_FULLADDRESS_TABLE SET PermanentAdd = @PermanentAdd, PermanentAdd2 = @PermanentAdd2, HomePlace = @HomePlace, HomeState= @HomeState, HomePin= @HomePin where EmployeeID = '" + global + "'", con); 
      cmd1.Parameters.AddWithValue("@PermanentAdd", TextBox1.Text); 

      cmd1.Parameters.AddWithValue("@PermanentAdd2", TextBox5.Text); 
      cmd1.Parameters.AddWithValue("@HomePlace", TextBox2.Text); 
      if (State.SelectedItem.Text == "--Select--") 
      { 
       State.SelectedItem.Text = TextBox3.Text; 
      } 
      cmd1.Parameters.AddWithValue("@HomeState", State.SelectedItem.Text); 
      cmd1.Parameters.AddWithValue("@HomePin", TextBox4.Text); 
      cmd1.ExecuteNonQuery(); 
      con.Close(); 

      lblmsg.Text = "DATA Updated Successfully"; 
      lblmsg.ForeColor = System.Drawing.Color.Green; 
     } 
     catch (Exception exp) 
     { 
      lblmsg.Text = exp.Message; 
      lblmsg.ForeColor = System.Drawing.Color.Red; 
     } 



    } 


    // static int count = 0; 
    protected void EditButton_Click(object sender, EventArgs e) 
    { 

       TextBox1.Enabled = true; 

       TextBox2.Enabled = true; 
       //TextBox3.Enabled = true; 
       TextBox4.Enabled = true; 
       TextBox5.Enabled = true; 
       State.Enabled = true; 

    } 

請幫助。

+0

恐怕你將不得不展示一些代碼。對此沒有「一般」解決方案。 – 2011-03-31 10:25:39

+0

聽起來像你沒有提交你的更新。 – 2011-03-31 10:25:50

+0

更新數據庫的代碼工作正常,當我不包括page_load函數內的select命令時。但是,只要我有page_load fn內的選擇命令,更新按鈕代碼工作正常,但沒有看到數據的變化在數據庫內。 請求代碼的哪個部分,請讓我知道。 – scooby 2011-03-31 10:27:37

回答

0

我想你已經評論了你的global/employeeid作業?

// global = Session["ID"].ToString();

你也應該將其更改爲您的SQL參數。

+0

嘿,克里斯,謝謝洛特。解決了這個謎團。全局字符串位於if(!IsPostBack)塊內部,因此它在頁面加載期間僅獲取一次值。所以在更新期間它只需要一個空值。問題解決了。 :) – scooby 2011-03-31 11:02:44