如果我选择带有 FileUpload 控件的照片或图像.. 记录更新过程将成功,但如果 FileUpload 控件没有选择文件出现上述错误,请记住路径正确且存在并且 FileUpload 控件保存图片而没有任何问题 。
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 ID=@ID";
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 + "'" + ",img=@FilePath 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);
}
}
}
我的代码哪里错了??请问有什么建议吗??
关于阿米尔。