Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView populate

Status
Not open for further replies.

neegottlob

Programmer
Mar 26, 2006
22
BG
Hello,
I've tried to populate my gridView from a database. The program starts but Gridview1 is empty. There is something wrong with tha way I build my dataTable I think. Hope somebody can give me advice or write any kind of a solutin.
Thank You!

p.s. Fname, SecondName, Phone, Lastname are fields from my Danni.mdb. And I think exactly there I have a mistake but I am not sure and I have absolutely no idea what is the mistake :)

Here is the source:

Code:
Dim connString As String = ConfigurationManager.ConnectionStrings("autodib").ConnectionString
 
            Dim connection As New OleDb.OleDbConnection(connString)
 
            Dim command As New OleDb.OleDbCommand("SELECT * from MarkaNaKola", connection)
 
            Dim adapter As New OleDb.OleDbDataAdapter(command)

            Dim authors As New DataSet()
            
            adapter.Fill(authors)
            
            If authors.Tables.Count > 0 Then
                
                Dim DTable As DataTable
                Dim row As DataRow
                'create a DataTable
                DTable = New DataTable
                DTable.Columns.Add(New DataColumn("FName", GetType(String)))
                DTable.Columns.Add(New DataColumn("SecondName", GetType(String)))
                DTable.Columns.Add(New DataColumn("LastName", GetType(String)))
                DTable.Columns.Add(New DataColumn("phone", GetType(String)))
                
                
                
                
                For Each row In authors.Tables(0).Rows
                    row = DTable.NewRow()
                    row(0) = row("FName").ToString()
                    row(1) = row("SecondName").ToString()
                    row(2) = row("LastName").ToString()
                    row(3) = row("phone").ToString()
                    DTable.Rows.Add(row)
                   
 
                Next
                GridView1.Enabled = False
                GridView1.DataSource = New DataView(DTable)
                GridView1.Enabled = True
                GridView1.DataBind()
                'Dim Column As Data.DataColumn
                'For Each Column In DTable.Columns
                ' Response.Write(Column.ColumnName & "<br>")
                ' Next

            End If
 
neegottlob - your SQL seems fine, but if there are additional fields I would spell out the column names in the SQL. Also I haven't seen much people using the word "connection" for a connection; generally I see "cnn" or "cnnq" etc. Of course neither of these are probably issues here.

My question is this. Why bind the Grid this way using a DataSet? This approach uses more server resources then say a simple binding of the Grid, e.g., using the following approach:
Code:
Dim cmdSelect As OLEDbCommand
Dim cnnRecs As OleDbConnection = New OleDbConnection( _
 "Provider=Microsoft.Jet.OLEDB.4.0; " & _
 "Data Source=" & Server.MapPath("fpdb\Records.mdb;"))
 cmdSelect = New OLEDbCommand("SELECT AwwSiteCode, Description, Type FROM tblImpbyYear WHERE intYear =" & Request.QueryString("intYr") & " AND Type='" & Request.QueryString("Type") & "'" & " ORDER BY Imp_Count DESC", cnnRecs)
Try         
 cnnRecs.Open()
 dgSites.DataSource = cmdSelect.ExecuteReader()
 dgSites.DataBind()
 cnnRecs.Close()
Catch ex as System.Exception
 Message.Text = ex.ToString()
Finally
 If (Not cnnRecs Is Nothing) Then
   cnnRecs.Close()
 End If
End Try
Not saying your approach is not valid, but not sure why you'd approach it this way. Also, what is the purpose of dienabling and enabling the GridView (just curious on that note). It appears at first glance all looks pretty good. What is the result, just the appearence of a Grid header with no data?
 
neegottlob - interestingly someone else has posted a thread today very similar to yours at thread855-1217044. Might take a look at that as well (and follow it).
 
Thanks for the replay!
Yes the reult is only the Grid Header and if I change the:
Code:
DTable.Columns.Add(New DataColumn("FName", GetType(String)))
with:
Code:
DTable.Columns.Add(New DataColumn("asd", GetType(String)))
I recieve err like:
Code:
Column 'FName' does not belong to table . 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Column 'FName' does not belong to table .

Source Error: 


Line 47:                 For Each row In authors.Tables(0).Rows
Line 48:                     row = DTable.NewRow()
Line 49:                     row(0) = row("FName").ToString()
Line 50:                     row(1) = row("SecondName").ToString()
Line 51:                     row(2) = row("LastName").ToString()
 

Source File: C:\Documents and Settings\gosho\My Documents\Visual Studio 2005\WebSites\WebSite3\Default3.aspx    Line: 49

That's why I think the problem comes from those rows.About the enableint and dienable that was sugget from a friend who solved the same problem like that but for me the "magic":) didn't work. And the whole idea of trying that stuff is because I want to make the Hyperlincs in the GridView with the names of the fields of the DataBase and not using that "select" part of the GridView. This is the only way I've in mind :)
 
nee: Try once removing the "*" from the SQL and see if that makes a difference. If no luck I'll run this code on a test page and see how it behaves.
 
Hi :)
I've tried this:

Code:
Dim command As New OleDb.OleDbCommand("SELECT [FName], [SecondName], [LastName], [phone] FROM [Member]", connection)

No difference :(

best regards,
Georgi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top