fileupload emty error !!!

kadir koçaslan 65 Reputation points
2023-01-30T11:57:08.06+00:00

Hello friends, if the file upload is empty

   SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Holding_DB"].ConnectionString);
                    con.Open();
                    SqlCommand cmdSelect = new SqlCommand("select * from TBL_Haber where id='" + id + "'", con);
                    SqlDataReader rdr = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection);
                    rdr.Read();
                    foto = rdr[4].ToString();
                    SqlCommand cmd = new SqlCommand("update TBL_Haber set HaberBaslik=@HaberBaslik,Haberİcerik=@Haberİcerik,Foto=@Foto where id=@id", con);
                    cmd.Parameters.AddWithValue("@HaberBaslik", txtBaslik.Text);
                    cmd.Parameters.AddWithValue("@Haberİcerik", txtAreaİcerik.Text);
                    cmd.Parameters.AddWithValue("@Foto", foto);
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.ExecuteNonQuery();
                    lblMessage.Text = "Haber Başarı İle Güncellendi ve Yayınlandı";

                    con.Close();
                    Page.Response.Redirect("Haberler.aspx");

Hello friends, if the file upload is full


                    FileInfo dosyaBilgisi = new FileInfo(yuklenecekDosya.FileName);
                    string klasor = "image";
                    string yuklemeYeri = Server.MapPath("~/Web-Holding/" + klasor + "/" + dosyaBilgisi.Name);
                    FileUpload2.SaveAs(yuklemeYeri);
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Holding_DB"].ConnectionString);
                    con.Open();
                    SqlCommand cmd = new SqlCommand("update TBL_Haber set HaberBaslik=@HaberBaslik,Haberİcerik=@Haberİcerik,Foto=@Foto where id=@id", con);
                    cmd.Parameters.AddWithValue("@HaberBaslik", txtBaslik.Text);
                    cmd.Parameters.AddWithValue("@Haberİcerik", txtAreaİcerik.Text);
                    cmd.Parameters.AddWithValue("@Foto", yuklenecekDosya.FileName);
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.ExecuteNonQuery();
                    lblMessage.Text = "Haber Başarı İle Güncellendi ve Yayınlandı";
                    Page.Response.Redirect("Haberler.aspx");
                    con.Close();

I want these codes to work, but I couldn't do it, I ask for your help.

all code on the page like this

 string foto = "";
                HttpPostedFile yuklenecekDosya = FileUpload2.PostedFile;
                
                if (FileUpload2.FileName != null)
                {

                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Holding_DB"].ConnectionString);
                    con.Open();
                    SqlCommand cmdSelect = new SqlCommand("select * from TBL_Haber where id='" + id + "'", con);
                    SqlDataReader rdr = cmdSelect.ExecuteReader(CommandBehavior.CloseConnection);
                    rdr.Read();
                    foto = rdr[4].ToString();
                    SqlCommand cmd = new SqlCommand("update TBL_Haber set HaberBaslik=@HaberBaslik,Haberİcerik=@Haberİcerik,Foto=@Foto where id=@id", con);
                    cmd.Parameters.AddWithValue("@HaberBaslik", txtBaslik.Text);
                    cmd.Parameters.AddWithValue("@Haberİcerik", txtAreaİcerik.Text);
                    cmd.Parameters.AddWithValue("@Foto", foto);
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.ExecuteNonQuery();
                    lblMessage.Text = "Haber Başarı İle Güncellendi ve Yayınlandı";

                    con.Close();
                    Page.Response.Redirect("Haberler.aspx");
                }
                if (FileUpload2.FileName == null)
                {

                    FileInfo dosyaBilgisi = new FileInfo(yuklenecekDosya.FileName);
                    string klasor = "image";
                    string yuklemeYeri = Server.MapPath("~/Web-Holding/" + klasor + "/" + dosyaBilgisi.Name);
                    FileUpload2.SaveAs(yuklemeYeri);
                    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Holding_DB"].ConnectionString);
                    con.Open();
                    SqlCommand cmd = new SqlCommand("update TBL_Haber set HaberBaslik=@HaberBaslik,Haberİcerik=@Haberİcerik,Foto=@Foto where id=@id", con);
                    cmd.Parameters.AddWithValue("@HaberBaslik", txtBaslik.Text);
                    cmd.Parameters.AddWithValue("@Haberİcerik", txtAreaİcerik.Text);
                    cmd.Parameters.AddWithValue("@Foto", yuklenecekDosya.FileName);
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.ExecuteNonQuery();
                    lblMessage.Text = "Haber Başarı İle Güncellendi ve Yayınlandı";
                    Page.Response.Redirect("Haberler.aspx");
                    con.Close();


                }
                else
                {

                    lblMessage.Text = "Bir Hata Oluştu Lütfen Web Master'a Başvurun";


                }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,010 questions
{count} votes

2 answers

Sort by: Most helpful
  1. kadir koçaslan 65 Reputation points
    2023-01-30T12:39:20.42+00:00

    abcd

    0 comments No comments

  2. kadir koçaslan 65 Reputation points
    2023-01-30T12:40:02.1466667+00:00

    abcd


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.