1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Vb.net datagrid

Discussion in 'C#' started by solarthur01@hotmail.com, Jan 21, 2008.

  1. #1
    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?
     
    solarthur01@hotmail.com, Jan 21, 2008 IP
  2. heyman

    heyman Peon

    Messages:
    70
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this:

    GridView1.Datasource = myreader
    GridView1.DataBind()
     
    heyman, Jan 21, 2008 IP
  3. teraeon

    teraeon Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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()
     
    teraeon, Jan 28, 2008 IP
  4. teraeon

    teraeon Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    teraeon, Jan 28, 2008 IP