2014-05-15 71 views
0
更新頁面

它在數據庫中更新,所以只有一個字段我是相當新的一般的編碼很抱歉,如果我的一些行話是錯誤的。ASP.net C#我怎樣才能使

我有一個存儲文件的圖像名稱在我的主頁,從而使管理員可以上傳新圖片來代替以前的圖像數據庫,以便張貼新上傳的圖片的網頁數據庫。

我目前遇到的問題是,當我只想更新其中一個圖像時,它會上傳該圖像,但其他圖像被替換爲空值,我只想要我上傳圖像的字段,更新。

這裏是我的代碼後面,我有一個公平的想法是我做錯了,但我真的不現在怎麼解決我的問題。任何幫助將不勝感激。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

using System.IO; 
using System.Data; 
using System.Data.SqlClient; 
using System.Web.Configuration; 

public partial class admin_updatehomepage : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void displayedit_ItemUpdated(object sender, ListViewUpdatedEventArgs e) 
    { 
     info.Text = "Item Updated"; 

     FileUpload fileupdate1 = displayedit.EditItem.FindControl("imageupdate1") as FileUpload; 
     FileUpload fileupdate2 = displayedit.EditItem.FindControl("imageupdate2") as FileUpload; 
     FileUpload fileupdate3 = displayedit.EditItem.FindControl("imageupdate3") as FileUpload; 
     FileUpload fileupdate4 = displayedit.EditItem.FindControl("imageupdate4") as FileUpload; 
     FileUpload fileupdate5 = displayedit.EditItem.FindControl("imageupdate5") as FileUpload; 
     FileUpload fileupdate6 = displayedit.EditItem.FindControl("imageupdate6") as FileUpload; 
     FileUpload fileupdate7 = displayedit.EditItem.FindControl("imageupdate7") as FileUpload; 
     FileUpload fileupdate8 = displayedit.EditItem.FindControl("imageupdate8") as FileUpload; 
     FileUpload fileupdate9 = displayedit.EditItem.FindControl("imageupdate9") as FileUpload; 

     Label recordid = displayedit.EditItem.FindControl("idlabel1") as Label; 
     Int32 id = Convert.ToInt32(recordid.Text); 

     if (fileupdate1.HasFile) 
     { 
      String fupload = fileupdate1.FileName; 

      Random r = new Random(); 
      int rInt = r.Next(0, 10000); 

      String imgpath = "../images/" + rInt + fupload; 

      fileupdate1.SaveAs(Server.MapPath(imgpath)); 

      String newimage = rInt + fupload; 

      string newsconnection = WebConfigurationManager.ConnectionStrings["newsconnection"].ConnectionString; 
      SqlConnection myConnection = new SqlConnection(newsconnection); 

      //myConnection.ConnectionString is now set to connectionString. 
      myConnection.Open(); 

      String query = "UPDATE homepage SET fballimg ='" + newimage + "' WHERE id='" + id + "'"; 

      SqlCommand myCommand = new SqlCommand(query, myConnection); 

      myCommand.ExecuteNonQuery(); 
      myConnection.Close(); 
     } 
     if (fileupdate2.HasFile) 
     { 
      String fupload = fileupdate2.FileName; 

      Random r = new Random(); 
      int rInt = r.Next(0, 10000); 

      String imgpath = "../images/" + rInt + fupload; 

      fileupdate2.SaveAs(Server.MapPath(imgpath)); 

      String newimage = rInt + fupload; 

      string newsconnection = WebConfigurationManager.ConnectionStrings["newsconnection"].ConnectionString; 
      SqlConnection myConnection = new SqlConnection(newsconnection); 

      //myConnection.ConnectionString is now set to connectionString. 
      myConnection.Open(); 

      String query = "UPDATE homepage SET rugbyimg ='" + newimage + "' WHERE id='" + id + "'"; 

      SqlCommand myCommand = new SqlCommand(query, myConnection); 

      myCommand.ExecuteNonQuery(); 
      myConnection.Close(); 
     } 
     if (fileupdate3.HasFile) 
     { 
      String fupload = fileupdate3.FileName; 

      Random r = new Random(); 
      int rInt = r.Next(0, 10000); 

      String imgpath = "../images/" + rInt + fupload; 

      fileupdate3.SaveAs(Server.MapPath(imgpath)); 

      String newimage = rInt + fupload; 

      string newsconnection = WebConfigurationManager.ConnectionStrings["newsconnection"].ConnectionString; 
      SqlConnection myConnection = new SqlConnection(newsconnection); 

      //myConnection.ConnectionString is now set to connectionString. 
      myConnection.Open(); 

      String query = "UPDATE homepage SET bballimg ='" + newimage + "' WHERE id='" + id + "'"; 

      SqlCommand myCommand = new SqlCommand(query, myConnection); 

      myCommand.ExecuteNonQuery(); 
      myConnection.Close(); 
     } 
    } 
    protected void displayedit_ItemEditing(object sender, ListViewEditEventArgs e) 
    { 
     info.Text = "<b>*Recommend images are 490px x 190px(or of similar ratio) to avoid image distortion</b>"; 
    } 
    protected void displayedit_ItemCanceling(object sender, ListViewCancelEventArgs e) 
    { 
     info.Text = "Not Updating"; 
    } 
} 
+0

我假設你正在圖像路徑存儲在這些領域。因此,在你的更新查詢中,你必須將其他兩個字段設置爲''''(_blank_或_empty string_)。 – deostroll

+0

@deostroll你的意思是這樣嗎? '串查詢= 「UPDATE SET主頁fballimg = '」 + newimage + 「 'rugbyimg = '',bballimg = '' WHERE ID ='」 + ID + 「'」;' – mcclosa

+0

是的。該表中只有三張圖像? – deostroll

回答

0

試試這個...

存放在不同的hiddenfield每個圖像的名稱....

現在在更新檢查:

if (fileupdateId.HasFile) 
{ 
    //then new file name 
} 
else 
{ 
    //else hiddenfield value 
} 

不要讓我知道是否有任何查詢..