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?
Dim connectionString As String = "provider=Microsoft.Jet.OLEDB.4.0;" + _ "data source = C:\Documents and Settings\admin2\My Documents\glaxodata.mdb" Dim myconnection As OleDbConnection myconnection = New OleDbConnection(connectionString) OleDbDataAdapter myadapter = new OleDbDataAdapter("select * from glaxoAll5032007OrderDetail", myconnection) DataSet mydataset = new DataSet() myadapter.fill( mydataset ) GridView1.Datasource = mydataset GridView1.DataBind()
it's better to use a data adapter in this instance so that you don't have to worry about opening and closing the connections and the reader. The adapter class will fill a data set which contains the data locally, the data reader is good for two instances. #1. More speed as it's forward reading only #2. When the you need to see changes to data as it's being read. Those instances are rare and you are moving it into a data table anyway so it's getting moved locally.