I have written a code as follows -
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- using System.IO;
- namespace Shop
- {
- public partial class addimage : System.Web.UI.Page
- { SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Sayan\\Documents\\Saanvi.mdf;Integrated Security=True;Connect Timeout=30");
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Buttonuploadimage_Click(object sender, EventArgs e)
- {
- FileUpload f = new FileUpload();
-
- if (f.HasFile)
- {
- string str = f.FileName;
- f.PostedFile.SaveAs(Server.MapPath(".") + "//Images//" + str);
- string path = "~//Images//" + str.ToString();
- con.Open();
- SqlCommand cmd=new SqlCommand("Insert into Images values('" + path + "')", con);
- cmd.ExecuteNonQuery();
- con.Close();
- Labeluploadimage.Text = "Image uploaded successfully";
- }
-
- else
- {
- Labeluploadimage.Text = "Pls. upload an image";
- }
However, I see that the if statement is not executed and always the statement under the else statement is executed. Is this something to do with the Button click event handler? I have a "upload image" button and a "upload" control used from the toolbox. I have an "Images" folder under solution explorer, where I have already kept few pics.