Displaying data in a gridview programatically
-
I am trying to display some data from a database withing a gridview. Using a few books, i have managed to display the data in a msngbox but im having problmes displaying it on the asp webpage. Heres what i have at the moment: Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myconnection As OleDbConnection myconnection = New OleDbConnection() Dim connectionString As String = "provider=Microsoft.Jet.OLEDB.4.0;" + _ "data source = C:\Documents and Settings\admin2\My Documents\glaxodata.mdb" myconnection.ConnectionString = connectionString Dim mycommand As OleDbCommand mycommand = New OleDbCommand("select * from glaxoAll5032007OrderDetail", myconnection) Dim myreader As OleDbDataReader myconnection.Open() myreader = mycommand.ExecuteReader() myreader.Read() Dim newrow As DataRow MsgBox(myreader("Field1")) Dim mytable As DataTable mytable = New DataTable() mytable.Rows.Add(newrow) MsgBox(mytable.Rows.Count) mytable.Rows.Add() GridView1.DataSource = mytable myconnection.Close() End Sub can any1 help?
-
I am trying to display some data from a database withing a gridview. Using a few books, i have managed to display the data in a msngbox but im having problmes displaying it on the asp webpage. Heres what i have at the moment: Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myconnection As OleDbConnection myconnection = New OleDbConnection() Dim connectionString As String = "provider=Microsoft.Jet.OLEDB.4.0;" + _ "data source = C:\Documents and Settings\admin2\My Documents\glaxodata.mdb" myconnection.ConnectionString = connectionString Dim mycommand As OleDbCommand mycommand = New OleDbCommand("select * from glaxoAll5032007OrderDetail", myconnection) Dim myreader As OleDbDataReader myconnection.Open() myreader = mycommand.ExecuteReader() myreader.Read() Dim newrow As DataRow MsgBox(myreader("Field1")) Dim mytable As DataTable mytable = New DataTable() mytable.Rows.Add(newrow) MsgBox(mytable.Rows.Count) mytable.Rows.Add() GridView1.DataSource = mytable myconnection.Close() End Sub can any1 help?
Hi! I dint understand why you are using ExecuteReader? It returns single record right? If you want to bind more than one record get data to datatable directly and bind to the gridview 'GridView1.DataSource = mytable' after this line add the following line 'GridView1.databind()'