file path is getting saved in database but image file is not saving in Empimages folder in project

umesh kumar 20 Reputation points
2023-09-04T11:58:10.9066667+00:00

I have developed a registeration page which is working fine but the problem is that path of the image is getting saved in server succesfully but image is not saving into the EmpImages folder in my project . Below is my code 

 

protected void Button1_Click(object sender, EventArgs e)
{

if(FileUpload1.HasFiles)
{
//if (FileUpload1.PostedFile != null)
//{
string fname = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("EmpImages") + fname);

con.Open();
FileUpload1.SaveAs(Server.MapPath("C:\Users\ISTC\source\repos\First\First\EmpImages\" + EmpImgName));
SqlCommand cmd = new SqlCommand("insert into [dbo].[Reg] (Id,Name,EmailId,Mobile,Password,EmpImgPath ) values(@Id,@Name,@EmailId,@Mobile,@Password,@EmpImgPath)", con);
cmd.Parameters.AddWithValue("Id", TextBox1.Text);
cmd.Parameters.AddWithValue("Name", TextBox2.Text);
cmd.Parameters.AddWithValue("EmailId", TextBox3.Text);
cmd.Parameters.AddWithValue("Mobile", TextBox4.Text);
cmd.Parameters.AddWithValue("Password", TextBox5.Text);
cmd.Parameters.AddWithValue("EmpImgName", fname);
cmd.Parameters.AddWithValue("EmpImgPath", "EmpImages/" + fname);

cmd.ExecuteNonQuery();
Response.Write("you have sucessfully registered");
Response.Redirect("Login.aspx");

}

 

i wrote following code in source code of image link <asp:Image ID="Image1" runat="server" ImageAlign="Middle" ImageUrl='<%#Eval("EmpImgPath","~/EmpImages/{0}")%>'/>

 

please provide the solution at the earliest for this

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,358 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 27,716 Reputation points Microsoft Vendor
    2023-09-05T02:51:28.74+00:00

    Hi @umesh kumar ,

    User's image

    You only need to use one of the methods in the picture above.

    The file name needs to be included in the Server.MapPath method.

     string fname = Path.GetFileName(FileUpload1.PostedFile.FileName);
     FileUpload1.PostedFile.SaveAs(Server.MapPath("~/EmpImages/" + fname));
    

    Since EmpImages/ is already included in the EmpImgPath parameter, the source code of the image link should be as follows:

     <asp:Image ID="Image1" runat="server" ImageAlign="Middle" ImageUrl='<%#Eval("EmpImgPath","~/{0}")%>'/>
    

    ALL code

     protected void Button1_Click(object sender, EventArgs e)
     {
       
         if (FileUpload1.HasFiles)
         {
             //if (FileUpload1.PostedFile != null)
             //{
             string fname = Path.GetFileName(FileUpload1.PostedFile.FileName);
             FileUpload1.PostedFile.SaveAs(Server.MapPath("~/EmpImages/" + fname));
    
             con.Open();
          
             SqlCommand cmd = new SqlCommand("insert into [dbo].[Reg] (Id,Name,EmailId,Mobile,Password,EmpImgPath ) values(@Id,@Name,@EmailId,@Mobile,@Password,@EmpImgPath)", con);
             cmd.Parameters.AddWithValue("Id", TextBox1.Text);
             cmd.Parameters.AddWithValue("Name", TextBox2.Text);
             cmd.Parameters.AddWithValue("EmailId", TextBox3.Text);
             cmd.Parameters.AddWithValue("Mobile", TextBox4.Text);
             cmd.Parameters.AddWithValue("Password", TextBox5.Text);
             cmd.Parameters.AddWithValue("EmpImgName", fname);
             cmd.Parameters.AddWithValue("EmpImgPath", "EmpImages/" + fname);
    
             cmd.ExecuteNonQuery();
             Response.Write("you have sucessfully registered");
             Response.Redirect("Login.aspx");
    
         }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful