fileupload emty error !!!

kadir koçaslan
40
Reputation points
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";
}
{count} votes
Hi @kadir koçaslan,
The code you give is about database updates and searches. I don't think the problem can be just here. There can also be problems with the use of the button control. I would like to give a few suggestions:
1.Complete your code. Currently your code only allows us to determine if there is a problem with the database operation. If you're sure anything else is okay, drop me a message and I'll test the database operation for you.
2.if (FileUpload2.FileName != null) and if (FileUpload2.FileName == null). All cases are included. No need to write another else.
3.I see that you have a redirect action behind each judgment. Maybe this causes you to not see the results.
4.If your FileUpload2.FileName is a string variable. The judgment should be (FileUpload2.FileName == "").
Best Regards
Qi You