2011-07-16 33 views
0

我有一個關於在數據庫中添加圖像URL的問題。我在ASP.Net中使用formupload中的fileupload方法。並且我有一個名爲duyurular 的表,它可以記錄圖像URL.BTW ,我正在使用SQL Server數據庫。 我的問題是;我正在進行更新,刪除和在FormView中發佈公告。我可以使用FileUpload將這些圖像上傳到名爲「img」的文件夾中。 但是,我想將它記錄在數據庫中。如果要在數據庫中添加另一個這些信息,則沒有圖像URL。 最後,我無法在數據庫中添加圖片網址。FormView中的FileUpload

這是我的代碼;

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

FileUpload dosya, dosya1; 
//TextBox t1, t2, t3; 
//Button btn; 
SqlConnection con; 
static string str = "Data Source=SERT;Initial Catalog=Mmakina;Integrated Security=True"; 
string yol = ""; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    dosya = (FileUpload)FormView2.FindControl("FileUpload1"); 
    dosya1 = (FileUpload)FormView2.FindControl("FileUpload2"); 
    // btn = (Button)FormView2.FindControl("ResimKaydetButonu"); 
    //t1 = (TextBox)FormView2.FindControl("DuyuruBaslikTextBox"); 
    //t2 = (TextBox)FormView2.FindControl("DuyuruIcerikTextBox"); 
    //t3 = (TextBox)FormView2.FindControl("DuyuruTarihiTextBox"); 

    Label1.Visible = false; 
    if (Session["KullaniciID"]!=null) 
    { 
     con = new SqlConnection(str); 
     SqlCommand sorgu = new SqlCommand("SELECT * FROM Kullanici WHERE [email protected]", con); 
     sorgu.Parameters.Add("@KullaniciAdi", SqlDbType.VarChar).Value = Session["KullaniciAdi"]; 
      con.Open(); 
     SqlDataReader oku = sorgu.ExecuteReader(CommandBehavior.CloseConnection); 
     Label1.Visible = true; 
     while (oku.Read()) 
     { 
      Label1.Text = oku["KullaniciAdi"].ToString(); 

     } 

    } 
    else { 
     Response.Redirect("error.aspx"); 
    } 


} 
protected void Button1_Click(object sender, EventArgs e) 
{ 
    FormsAuthentication.SignOut(); 
    Roles.DeleteCookie(); 
    Session.Clear(); 
    Response.Redirect("giris.aspx"); 

} 

protected void btn_Click(object sender,EventArgs e) { 
    try 
    { 
     if (dosya.HasFile) 
     { 
      dosya.SaveAs(Server.MapPath("~/img/") + dosya.FileName); 
      System.Drawing.Image resim = System.Drawing.Image.FromFile(Server.MapPath("~/img/") + dosya.FileName); 
      yol = "img/" + dosya.FileName; 
      resim.Dispose(); 
      DbUpload(); 


     } 
    } 
    catch (Exception ex) 
    { 

    } 
} 



public void DbUpload() { 
    try { 
        SqlConnection sc = new SqlConnection("Data Source=SERT;Initial Catalog=Mmakina;Integrated Security=True"); 
     SqlCommand scom = new SqlCommand("insert into Duyuru(DuyuruResmi) values(@DuyuruResmi)", sc); 
     scom.Parameters.AddWithValue("@DuyuruResmi", dosya.FileName); 

     scom.ExecuteNonQuery(); 
     sc.Close(); 
    }catch(Exception p){ 
     p.Message.ToString(); 
    } 
} 


protected void UpdateButton_Click(object sender, EventArgs e) 
{ 
try 
    { 

     if (dosya.HasFile) 
     { 
      dosya.SaveAs(Server.MapPath("~/img/") + dosya.FileName); 
      yol = "img/" + dosya.FileName; 
      Response.Write("Fileupload çalışıyor..."); 
     DbUpload(); 
     } 
    } 
    catch (Exception ex) 
    { 
    } 
    }  

在此先感謝您分享的所有意見。

+0

總是嘗試發佈相關代碼並正確格式化。 –

+0

有什麼建議嗎? – Selo

回答

0

您必須使用Formview ItemInserting Event,您可以在其中傳遞構建的URL。

protected void frmAsset_ItemInserting(object sender, FormViewInsertEventArgs e) 
{ 
    if (dosya.HasFile) 
    { 
     dosya.SaveAs(Server.MapPath("~/img/") + dosya.FileName); 
     e.NewValues["URL"] = "img/" + dosya.FileName; 
    } 
} 
+0

- 首先,感謝您的有趣。但是,這是給我的錯誤和頁面無法打開。 – Selo

+0

編譯器錯誤消息:CS1061:'System.Web.UI.WebControls.FormViewUpdateEventArgs'不包含'Values'的定義,也沒有包含接受類型爲「System.Web.UI.WebControls.FormViewUpdateEventArgs」的第一個參數的擴展方法'Values' '可以找到(你是否缺少一個使用指令或裝配參考?) – Selo

+0

Selo;對不起,這在ItemUpdating事件中使用。只是錯過了它。這裏應該是NewValues,我剛剛更新了我的答案。 –

0

我建議你剛剛上傳的圖片名稱,而不指定完整的URL,並且可以保存在像'<add key="ImagesBasePath" value="/img" />'的web.config中的圖像基本路徑,從而被你有你的形象,你可以改變路徑可以通過將圖像名稱連接到ConfigurationManager.AppSettings["ImagesBasePath"]來控制此圖像的視圖,所以這會更好。