how to write a vb.net code for asp.net to display image in gridview by using the url of the image stored as text in sql database.So whatever the url may be the gridview should show the image using that text as reference instead of just using the name of the image....

is there a way..?

it says only to specify the field name and the function as a datasource...didnt help...

heres my asp code:]

<asp:GridView ID="GridView2" runat="server" BackColor="White" 
            BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4">

            <Columns>
                <asp:TemplateField>
                <ItemTemplate><img border=2 width=40%  align=middle src="~\<%#Container.DataItem("img_URL")%>" /></ItemTemplate> 
                   </asp:TemplateField>
                <asp:ImageField DataImageUrlField="img_URL" HeaderText="image">
                </asp:ImageField>
            </Columns>

            <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
            <RowStyle BackColor="White" ForeColor="#003399" />
            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
            <SortedAscendingCellStyle BackColor="#EDF6F6" />
            <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
            <SortedDescendingCellStyle BackColor="#D6DFDF" />
            <SortedDescendingHeaderStyle BackColor="#002876" />
        </asp:GridView>

and vb code for datasource:

Protected Sub GridView2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView2.Load
        Dim conn As New SqlConnection
        conn.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & Server.MapPath("~\App_Data") & "\ICUdb612.mdf;Integrated Security=True;User Instance=True"
        conn.Open()



    Dim sqlUserName As String
    sqlUserName = "SELECT * FROM posts WHERE Username='" & Session("UserName") & "'"
    Dim cmd As New SqlCommand(sqlUserName, conn)


    Dim data As New DataTable

    Dim cmd1 As New SqlCommand(sqlUserName, conn)
    Dim da As New SqlDataAdapter(cmd)
    Dim ds As New DataSet
    da.Fill(ds)

    conn.Close()
    If ds.Tables(0).Rows.Count > 0 Then
        GridView2.DataSource = ds
        GridView2.DataBind()

    Else
        ds.Tables(0).Rows.Add(ds.Tables(0).NewRow())
        GridView2.DataSource = ds
        GridView2.DataBind()
        Dim columncount As Integer = GridView2.Rows(0).Cells.Count
        GridView2.Rows(0).Cells.Clear()
        GridView2.Rows(0).Cells.Add(New TableCell())
        GridView2.Rows(0).Cells(0).ColumnSpan = columncount
        GridView2.Rows(0).Cells(0).Text = "No Records Found"
    End If


End Sub

I dont have much time at the moment to give you a detailed answer, but I quickly copied this from a page that I previously worked on. In my particular example, I did not add any additional code in the code behind page to fill the gridview. I used a SQLDataSource control on the aspx page.

<asp:ImageField DataImageUrlField="status" 
     DataUrlFormatString="/images/{0}.png" HeaderText="Status">
</asp:ImageField>

The name of the picture file was stored in a table in a field called "status". As you can see, I used both the DataImageUrlField as well as DataUrlFormatString to supply the Image details.

If that's not useful, I'll try to get a working example.

Well Thanks for your time,

I am familiar with this method ,which i can use if i am storing all images in one single folder....
but issue is I need a flexible way that will read the folder address(URl) from the field and then use it as imageurl to display ...

Maybe this myt be a complex concept...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.