Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: SQL-Server VB.NET
I want to store row for row which a datareader reads from my sql database into a gridview.
basically :
While reader.Read()
//Here i want to get the entire row that it reads eg. theName,Description,number
//and store that as is so that it can be used to populate my gridview.
end while
The gridview must look exactly like a SQL table would when you run a query like :
SELECT * FROM tblClients
any help?
Posted 22-Aug-12 23:59pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

A simple binding can't be an option???
public static DataTable GetData()
    DataTable table = new DataTable();
    string query = @"SELECT * FROM tblClients";
    using (SqlConnection cn = new SqlConnection(Configuration.DefaultConnectionString))
        using (SqlCommand cmd = new SqlCommand(query, cn))
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
    return table;
MyGrid.DataSource = GetData();
Public Shared Function GetData() As DataTable
	Dim table As New DataTable()
	Using cn As New SqlConnection("your connection string")
		Using cmd As New SqlCommand("SELECT * FROM tblClients", cn)
			Using reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
			End Using
		End Using
	End Using
	Return table
End Function
Member 9374423 at 23-Aug-12 5:12am
Hi I do appreciate the help. but could you please rewrite this in code? it seems to have trouble descripting in VB.
Mario Majčica at 23-Aug-12 5:16am
I just updated the solution. Converting code is easy or .
Member 9374423 at 23-Aug-12 5:20am
It works Perfectly Thank you very much :}
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The FieldCount property of the DataReader tells you the number of columns.
The GetFieldType(columnIndex) function gives infromation on the type of the data.
And then do the nested loops thru your data, and fill the gridview.
Member 9374423 at 23-Aug-12 5:14am
could you please supply an example?

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

  Print Answers RSS
0 Peter Leow 390
1 Sergey Alexandrovich Kryukov 330
2 Zoltán Zörgő 258
3 CHill60 195
4 CPallini 155
0 Sergey Alexandrovich Kryukov 8,943
1 OriginalGriff 6,631
2 Peter Leow 4,102
3 Zoltán Zörgő 3,714
4 Kornfeld Eliyahu Peter 2,525

Advertise | Privacy | Mobile
Web02 | 2.8.150123.1 | Last Updated 23 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100