找不到路径的一部分.使用上传屁股更新 sql

本文关键字:屁股 更新 sql 路径 一部分 找不到 | 更新日期: 2023-09-27 18:34:01

如果我选择带有文件上传控件的照片或图像..记录更新过程将成功,但如果文件上传控件没有选择文件出现上述错误,请记住路径正确且存在,并且文件上传控件保存图片没有任何问题。

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);
        }
    }
}

我的代码哪里有问题?? 请提供任何建议??

问候阿米尔。

找不到路径的一部分.使用上传屁股更新 sql

因为根据您的代码,文件上传永远不会为空。尝试验证代码:

if(fileUpload.HasFile && fileUpload.PostedFile.ContentLength>0) // check for 
                                                                 validity of file
{
  var path = string.Format("~/YourImageDir/{0}",Guid.NewGuid().ToString().
  Replace("-",string.Empty));     
  //then do your update with this above path 
}
else
{
 // update without file path
}