1

I make an application in asp.net in registration page i want to upload the picture on that page and save in database simultaneously but the database work has done i m able to save my upload image but can't show on that page.

My designing code :

 <form id=`form 1`  run at="server">
    <div>
    <table>
    <tr>
    <td>
         <asp:FileUpload ID="FileUpload1" run at="server" />
    </td>
    <td>
        <asp:Button ID="Button1" runat="server" Text="submit" onclick="Button1_Click" /></td>
        <td>
            <asp:Label ID="Label1" runat="server" Text="" Font-Names = "Arial"></asp:Label>
            <asp:Image ID="Image1" runat="server" Height="52px" Width="79px" />
        </td></tr></table>

    </div>
    </form>

my code behind :

    protected void Button 1_Click(object sender, EventArgs e)
    {
        // Read the file and convert it to Byte Array
        string filePath = FileUpload1.PostedFile.FileName;
        string filename = Path.GetFileName(filePath);
        string ext = Path.GetExtension(filename);
        string contenttype = String.Empty;
        Image1.ImageUrl = filename;
        //Set the contenttype based on File Extension
        switch (ext)
        {
            case ".doc":
                contenttype = "application/vnd.ms-word";
                break;
            case ".docx":
                contenttype = "application/vnd.ms-word";
                break;
            case ".xls":
                contenttype = "application/vnd.ms-excel";
                break;
            case ".xlsx":
                contenttype = "application/vnd.ms-excel";
                break;
            case ".jpg":
                contenttype = "image/jpg";
                break;
            case ".png":
                contenttype = "image/png";
                break;
            case ".gif":
                contenttype = "image/gif";
                break;
            case ".pdf":
                contenttype = "application/pdf";
                break;
        }
        if (contenttype != String.Empty)
        {

            Stream fs = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);

            //insert the file into database
            string strQuery = "insert into empimage(Name, ContentType, Data)" +
               " values (@Name, @ContentType, @Data)";
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
            cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value
              = contenttype;
            cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
            InsertUpdateData(cmd);
            Label1.ForeColor = System.Drawing.Color.Green;
            Label1.Text = "File Uploaded Successfully";
        }
        else
        {
            Label1.ForeColor = System.Drawing.Color.Red;
            Label1.Text = "File format not recognised." +
              " Upload Image/Word/PDF/Excel formats";
        }

}
    private Boolean InsertUpdateData(SqlCommand cmd)
    {
        pd = ConfigurationManager.ConnectionStrings["su"].ConnectionString;
        con = new SqlConnection(pd);
        //String strConnString = System.Configuration.ConfigurationManager
        //.ConnectionStrings["conString"].ConnectionString;
        //SqlConnection con = new SqlConnection(strConnString);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            return false;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }

but it does not show the image at run time

please suggest me

Krunal Patil
  • 3,666
  • 5
  • 22
  • 28
user3627833
  • 41
  • 1
  • 7
  • Please refer [this link][1]. Answer to your question is described right here. [1]: http://stackoverflow.com/questions/4665160/image-url-is-correct-but-image-not-showing – Robin Joseph May 21 '14 at 06:54

1 Answers1

0

you have to try like

Image1.ImageUrl = FileUpload1.FileName;
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
  • 1
    Can you please explain in brief how your solution works. It would make your answer more helpful(+1s) for OP and other readers. – Mohit Jain May 21 '14 at 07:13
  • i try this code but it not working it only show the image icon but not image but i find answer of my question the code is protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") { FileUpload1.SaveAs(Server.MapPath("image/" +FileUpload1.FileName)); Image1.ImageUrl = "~/image/" + FileUpload1.FileName; } cmd =new SqlCommand("insert into empimage(Name) values('"+"~/Image/"+ FileUpload1.FileName+"')",con); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } – user3627833 May 21 '14 at 09:58