Help! uploading image into web folder and store image name into database SQL Server 2000 (ASP.NET 2.0) RRS feed

  • Question

  • User-198142416 posted


     This is my situation:

     I have three columns  and image name column (4 colums in total) to be inserted into SQL SERVER database. I  want upload image and store into web folder  called "publicfolder" which I did but also I wnat that image name to be inserted into table in MS SQL SERVER 2000 as well as retrieve image from image folder using image name from database . I will appreciate your help:

    This is my code:

    ASP.NET 2.0


     inserting into database:

    Protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add.Click


    SqlDataSource1.InsertParameters.Add("Firstname", Txtfirstname.Text.ToString()) SqlDataSource1.InsertParameters.Add("Lastname", Txtlastname.Text.ToString())

    SqlDataSource1.InsertParameters.Add("Email", TxtEmail.Text.ToString())

    SqlDataSource1.InsertParameters.Add("Imagename", upImage.FileName.ToString())

     End Sub


    upload image 

     Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)


     If upImage.HasFile Then

    Dim SavePath As String = (Server.MapPath("~/Publicfolder/") & "\" & upImage.FileName)

     lblImgname.Text = upImage.FileName

     Response.Write("Image File Name <b>" & upImage.FileName & "</b> <br>")

     End If

    Catch ex As Exception

     Throw ex

     End Try

    End Sub

    Display data and image on asp page:

    asp:GridView  <Columns> <asp:TemplateField> <ItemTemplate> <div style="width:100%">

    <table cellspacing="0">

    asp:Label ID="Label3" runat="server" Text='<%# Eval("Firstname") %>' /> <asp:Label ID="Label4" runat="server" Text='<%# Eval("Lastname") %>' ></asp:Label></span>

    how do I display image  from database?



    </ItemTemplate><FooterTemplate></asp:TemplateField></Columns> </asp:GridView>

     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDB%>"

    InsertCommand="INSERT INTO [MYDB] ([Firstname],[Lastname], [Email],  imagename) VALUES (@Firstname, @Lastname, @Email,  @imagename)">



    Thanks in advance.

    Tuesday, April 7, 2009 2:52 PM

All replies

  • Wednesday, April 8, 2009 4:23 AM
  • User-198142416 posted

    Hello Vora,

     I look at the link but that is not what I was looking for. I have not problem loading image directly into database, but I want is may I was not clear, here is what I need to do:

     I have page that users have to use to provide their name and email as well as thier picture:

    I have a table with four columns: firstname, lastname, email and imagename

    1. image stored into a folder called" publicfolder" locate web root

    2. insert data for firstname, lastname , email and image pathname into the table.

    3. retrieve firstname , lastname, email, and imagename from table . and imagename from table should also get image from "publicfolder" folder in web root.

    4. display all on GRiedview.





    4. while  

    Wednesday, April 8, 2009 8:39 AM
  • User-339429321 posted


               You just save the file name in database so that you can retrieve the imagefilename and bind it to any data display controls like datalist or grid view.

    you can save filename in database with relation to userid i.e have 2 columns in database

    ImageName nvarchar(20),

    user id int not null..

     when ur saving the file in Image folder Pmap ,use this format







    and save the lbluploadresume.text in database so that you with the uploaded filename userid will be attached so that if u have same filename for different users the new file will replace the old one.

    when Your are retreiving the Image  use this code

    <Img src='Foldername/<%#DataBinder.Eval(Container.DataItem,"ImageName")%>'

    Monday, July 13, 2009 3:19 AM