Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI,
I would like to take data from database and show it in DataGrid. I used auto generated columns false and "PictureURL" is image field, but this gives empty DataGrid.

Please help.
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim Con As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString").ToString())
        Con.Open()
        Dim Sel As String = "Select * from Item"
        Dim cmd As New SqlCommand(Sel, Con)

        Dim dt As New DataTable()
        dt.Columns.Add(New DataColumn("PictureID", GetType(Integer)))
        dt.Columns.Add(New DataColumn("PictureURL", GetType(String)))
        dt.Columns.Add(New DataColumn("Title"", GetType(String)))
        dt.Columns.Add(New DataColumn("DateAdded";, GetType(DateTime)))
        dt.Columns.Add(New DataColumn("Price"", GetType(Decimal)))
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        While reader.Read()
            Dim dr As DataRow = dt.NewRow()
            dr("PictureID") = Convert.ToInt32(reader("PictureID"))
            dr("PictureURL") = ResolveUrl("~/images/" + reader("PictureURL"))
            dr("Title") = reader("Title")
            dr("DateAdded") = reader("DateAdded")
            dr("Price") = reader("Price")
            dt.Rows.Add(dr)
        End While
        Con.Close()
         GridView1.DataSource = dt
        GridView1.DataMember = "Item"
        GridView1.DataBind()


    End Sub
Posted
Updated 16-Nov-11 0:15am
v2
Comments
Sergey Alexandrovich Kryukov 16-Nov-11 12:37pm    
Are you sure this is DataGrid? Full name of class, please. WPF?
--SA

1 solution

You do it all manually, using immediate string constants hard-coded in the code, so what do you expect? At least run it under debugger and see what's going on.

First, you data set could be empty, check it up. If it is not empty, binding names of your columns in you grid view (whatever it is, you don't show exact type of a component) and data table column names could mismatch. You can check up everything under debugger.

Now, the question is: if you want to fill the same data set or data table from database again, what, are you going to create a data table again and again? Also, did you ever heard of data adapter classes? :-)

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900