2014-01-21 138 views
1

我正在爲大學一個項目,我想說的是,我在asp.net很新。ASP.NET C#更新查詢不工作

請找到下面的代碼,我遇到的問題。問題是更新功能不起作用。在頁面加載中,我有一些選擇查詢,並將數據從數據庫加載到一些textareas和textfields中。這工作正常 - 它會加載手動添加到我的數據庫中的示例數據。

我有按鈕應該更新點擊數據庫。

這是按鈕的代碼:

<a id="A1" class="button" onserverclick="box1_Click" runat="server"> 
     <span>Запази полето <img src="notification-tick.gif" width="12" height="12" /></span> 
     </a> 

這是後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 


public partial class admin_Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     //Loading the data from the database 

     string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["proekt"].ConnectionString; 
     string sql1_title = "SELECT title FROM home WHERE id=1"; 
     string sql1_image = "SELECT image FROM home WHERE id=1"; 
     string sql1_text = "SELECT text FROM home WHERE id=1"; 

     string sql2_title = "SELECT title FROM home WHERE id=2"; 
     string sql2_image = "SELECT image FROM home WHERE id=2"; 
     string sql2_text = "SELECT text FROM home WHERE id=2"; 

     string sql3_title = "SELECT title FROM home WHERE id=3"; 
     string sql3_image = "SELECT image FROM home WHERE id=3"; 
     string sql3_text = "SELECT text FROM home WHERE id=3"; 
     using (SqlConnection conn = new SqlConnection(DatabaseConnectionString)) 
     { 
      //box1 data load 
      SqlCommand cmd1_title = new SqlCommand(sql1_title, conn); 
      conn.Open(); 
      box1_title.Text = (string)cmd1_title.ExecuteScalar(); 
      conn.Close(); 
      SqlCommand cmd1_image = new SqlCommand(sql1_image, conn); 
      conn.Open(); 
      box1_img.Text = (string)cmd1_image.ExecuteScalar(); 
      conn.Close(); 
      SqlCommand cmd1_text = new SqlCommand(sql1_text, conn); 
      conn.Open(); 
      box1_text.InnerText = (string)cmd1_text.ExecuteScalar(); 
      conn.Close(); 

      //box2 data load 
      SqlCommand cmd2_title = new SqlCommand(sql2_title, conn); 
      conn.Open(); 
      box2_title.Text = (string)cmd2_title.ExecuteScalar(); 
      conn.Close(); 
      SqlCommand cmd2_image = new SqlCommand(sql2_image, conn); 
      conn.Open(); 
      box2_img.Text = (string)cmd2_image.ExecuteScalar(); 
      conn.Close(); 
      SqlCommand cmd2_text = new SqlCommand(sql2_text, conn); 
      conn.Open(); 
      box2_text.InnerText = (string)cmd2_text.ExecuteScalar(); 
      conn.Close(); 

      //box3 data load 
      SqlCommand cmd3_title = new SqlCommand(sql3_title, conn); 
      conn.Open(); 
      box3_title.Text = (string)cmd3_title.ExecuteScalar(); 
      conn.Close(); 
      SqlCommand cmd3_image = new SqlCommand(sql3_image, conn); 
      conn.Open(); 
      box3_img.Text = (string)cmd3_image.ExecuteScalar(); 
      conn.Close(); 
      SqlCommand cmd3_text = new SqlCommand(sql3_text, conn); 
      conn.Open(); 
      box3_text.InnerText = (string)cmd3_text.ExecuteScalar(); 
      conn.Close(); 
     } 
    } 

    protected void box1_Click(object sender, EventArgs e) 
    { 
     string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["proekt"].ConnectionString; 
     string sql1 = "UPDATE home SET [email protected], [email protected], [email protected] WHERE Id=1"; 
     using (SqlConnection conn = new SqlConnection(DatabaseConnectionString)) 
     { 
      SqlCommand cmd2 = new SqlCommand(sql1, conn); 

      cmd2.Parameters.AddWithValue("@title", box1_title.Text); 
      cmd2.Parameters.AddWithValue("@image", box1_img.Text); 
      cmd2.Parameters.AddWithValue("@text", box1_text.InnerText); 

      conn.Open(); 
      cmd2.ExecuteNonQuery(); 
      conn.Close(); 
     } 
    } 

    protected void box2_Click(object sender, EventArgs e) 
    { 

    } 


    protected void box3_Click(object sender, EventArgs e) 
    { 

    } 
} 

當我做出改變的BOX1的標題,然後單擊該按鈕更新數據庫實際上是刷新頁面並再次加載示例數據,而我的更改未保存。

你能幫我解決這個問題嗎?沒有任何錯誤。

非常感謝你們。

PS:我注意到當我在瀏覽器中加載頁面,然後刪除數據加載的整個代碼塊時,在其中一個字段中對瀏覽器進行更改,然後按實際更新的按鈕數據庫。這是很奇怪......

+3

避免** 9 **跳到數據庫; 'SELECT id,title,image,text FROM home WHERE id in(1,2,3)order by id' and filter in the client –

+0

在一個建議中,打開連接一次,一次完成所有查詢,然後關閉它。 – StuartLC

回答

4

這是因爲你需要採取有關ASP.NET lifecycle照顧。

您需要檢查其是否爲PostBack(單擊按鈕時發生回傳)或者您沒有... 或者您將會覆蓋您的數據。

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!PostBack) 
    { 
      string DatabaseConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["proekt"].ConnectionString; 
      string sql = "SELECT id,title,image,text FROM home WHERE id in (1,2,3) order by id";  
      using (SqlConnection conn = new SqlConnection(DatabaseConnectionString)) 
      { 
      conn.Open(); 

搜索的DataReader - >這裏使用它...的ID,如果它的項目/標題等返回的數據保存到 DataReader的和過濾像herehere

  conn.Close(); 
      } 

    }  
} 
+0

你打我:) –

+0

非常感謝你! 最好打開連接然後關閉它,因爲我多次打開和關閉? – Berchev

+0

@Berchev在開始時只需打開DataBase一次,最後關閉它,在完成所有事務之後(我會在我的答案中給出一個示例)。如果我的回答是正確的,請將其標記爲答案;) – DatRid

0

最前一頁,我想建議在Page_Load中,只打開一次sql連接,並在連接打開時執行所有命令,然後在最後關閉它。這會讓你的網頁加載速度更快。

其次,你可以用asp LinkBut​​ton的點擊時創建一個回傳。語法應該看起來像這樣:

<asp:LinkButton ID="A1" runat="server" OnClick="box1_Click"> 
    <span>Запази полето <img src="notification-tick.gif" width="12" height="12" /></span> 
</asp:LinkButton> 
+0

謝謝你的建議我會嘗試一下。對於按鈕,我使用的是「a」,因爲我使用的CSS模板需要此樣式。 – Berchev

+0

在將數據寫入數據庫之前對字段的文本進行編碼還是強制性的,或者是否使用了定義性的查詢? – Berchev

+0

您可以像這樣將一個css類添加到LinkBut​​ton中:CssClass =「button」。 – robert00769