0
如果我選擇照片或圖像與FileUpload控制..記錄更新過程將成功,但如果FileUpload控件沒有文件選擇上述錯誤出現,要記住路徑是正確的,存在和FileUpload Control保存圖片沒有任何問題。找不到路徑的一部分。使用上傳按鈕更新sql
protected SqlCommand News_command;
protected SqlDataAdapter News_adp;
protected System.Data.DataTable News_tbl;
protected SqlConnection _connection;
protected string _ID;
protected void Page_Load(object sender, EventArgs e)
{
if ((Request.QueryString["ID"] != null))
{
_ID = Request.QueryString["ID"].ToString();
}
prepareConnection();
News_command.CommandText = "select * from News where [email protected]";
News_command.Parameters.AddWithValue("ID", _ID);
News_adp = new SqlDataAdapter();
News_tbl = new System.Data.DataTable();
News_adp.SelectCommand = News_command;
News_adp.Fill(News_tbl);
if (News_tbl.Rows.Count > 0)
{
lblID.Text = News_tbl.Rows[0]["ID"].ToString();
titleTextBox.Text = News_tbl.Rows[0]["Title"].ToString();
CKEditor1.Text = News_tbl.Rows[0]["Contect"].ToString();
imgArticle.ImageUrl = News_tbl.Rows[0]["img"].ToString();
lblDate.Text = News_tbl.Rows[0]["Date"].ToString();
}
}
protected void prepareConnection()
{
_connection = new SqlConnection(@"Data Source=Abu-Adam\localhost;Initial Catalog=BrainStorms;User ID=sa;Password=ameer123");
_connection.Open();
News_command = new SqlCommand();
News_command.Connection = _connection;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile == null)
{
prepareConnection();
News_command.CommandText = "UPDATE News SET Title=" + "N'" + titleTextBox.Text + "'" + "," + "Contect=" + "N'" + CKEditor1.Text + "'" + " WHERE ID='" + Convert.ToInt16(lblID.Text) + "';";
News_command.ExecuteNonQuery();
}
else if (FileUpload1.PostedFile != null)
{
prepareConnection();
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
//save file to disk
FileUpload1.SaveAs(Server.MapPath("~/ArticleImages/News/" + FileName));
News_command.CommandText = "UPDATE News SET Title=" + "N'" + titleTextBox.Text + "'" + "," + "Contect=" + "N'" + CKEditor1.Text + "'" + ",[email protected] WHERE ID='" + Convert.ToInt16(lblID.Text) + "';";
News_command.Parameters.AddWithValue("FilePath", "~/ArticleImages/News/" + FileName);
try
{
News_command.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
else
{
try
{
News_command.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
哪裏是我的代碼錯了??請提出任何建議?
關於 Ameer。
我解決了它 更換,如果(FileUpload1.PostedFile!= NULL)與IF(FileUpload1.HasFile) –