Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

datagridview

Posted on 2006-05-11
9
Medium Priority
?
983 Views
Last Modified: 2008-02-01
I am using VB.net 2005 and can't get my data to load to DataGridview. Please help.

Private Sub btnFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFill.Click



Dim StrConnection As String = "provider=microsoft.jet.oledb.4.0;" + "data source = C:\Documents and Settings\XMAN.SST_DOM\Desktop\ADDRESS\bad.mdb"

Dim Connection As New OleDbConnection(StrConnection)

Dim da As New OleDbDataAdapter

Dim Command1 As String = "select *from badtable"

Dim myCommand As New OleDbCommand

Dim ds As New DataSet



Try

            da.SelectCommand = New OleDbCommand(Command1, Connection)

            Connection.Open()

            myCommand.ExecuteNonQuery()

            Connection.Close()

            da.Fill(ds)

            DataGridView1.DataSource = ds.Tables("badtable")

        Catch ex As OperationAbortedException

            MsgBox(ex.ToString)


        End Try
0
Comment
Question by:VBdotnet2005
  • 4
  • 3
  • 2
9 Comments
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 16662506
when you call the fill method of a dataadapter by default it places the data in a table called "Table"

you can either change your datasource assignment to match or add a tablemapping for the dataadapter

da.tablemappings.add("Table", "badtable")
0
 

Author Comment

by:VBdotnet2005
ID: 16662575
correct?

Dim StrConnection As String = "provider=microsoft.jet.oledb.4.0;" + "data source = C:\Documents and Settings\XMAN.SST_DOM\Desktop\ADDRESS\bad.mdb"

Dim Connection As New OleDbConnection(StrConnection)

Dim da As New OleDbDataAdapter

Dim Command1 As String = "select *from badtable"

Dim myCommand As New OleDbCommand

Dim ds As New DataSet



Try

            da.SelectCommand = New OleDbCommand(Command1, Connection)

            Connection.Open()

            myCommand.ExecuteNonQuery()

            Connection.Close()

            da.Fill(ds)

            DataGridView1.DataSource = da.tablemappings.add("Table", "badtable")

        Catch ex As OperationAbortedException

            MsgBox(ex.ToString)


        End Try
0
 

Author Comment

by:VBdotnet2005
ID: 16662599
ERROR - ExecuteNonQuery: Connection property has not been initialized.


This line -   myCommand.ExecuteNonQuery()
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:maralans
ID: 16662626
Public Class Form1

    Dim cs As String = "provider=microsoft.jet.oledb.4.0;data source =C:\Documents and Settings\XMAN.SST_DOM\Desktop\ADDRESS\bad.mdb"
    Dim cn As New OleDb.OleDbConnection(cs)
    Dim da As New OleDb.OleDbDataAdapter(" "select *from badtable", cn)
    Dim ds As New DataSet

 

    Private Sub btnfill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfill.Click

        da.Fill(ds)

        dgv1.DataSource = ds.Tables(0)

       
    End Sub

End Class
0
 
LVL 5

Expert Comment

by:maralans
ID: 16662674

Public Class Form1

or:


 Private Sub btnfill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfill.Click
 
 Dim cs As String = "provider=microsoft.jet.oledb.4.0;data source =C:\Documents and  Settings\XMAN.SST_DOM\Desktop\ADDRESS\bad.mdb"
 Dim cn As New OleDb.OleDbConnection(cs)
 Dim da As New OleDb.OleDbDataAdapter(" "select *from badtable", cn)
 Dim ds As New DataSet

        da.Fill(ds)

        dgv1.DataSource = ds.Tables(0)
       
    End Sub

End Class
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 16662727
the dataset should be declared at the class/form level otherwise it will fall out of scope.  I would recommend the same for the dataadapter.

private ds as new dataset
private da as new oledbdataadapter

...

Dim StrConnection As String = "provider=microsoft.jet.oledb.4.0;" + "data source = C:\Documents and Settings\XMAN.SST_DOM\Desktop\ADDRESS\bad.mdb"
Dim Connection As New OleDbConnection(StrConnection)
Dim myCommand As New OleDbCommand("SELECT * FROM badtable", Connection)

da.selectcommand = myCommand
da.tablemappings.add("Table", "badtable")

Try
       da.Fill(ds)
       DataGridView1.DataSource = da.tablemappings.add("Table", "badtable")
Catch ex As OperationAbortedException
       MsgBox(ex.ToString)
End Try
0
 

Author Comment

by:VBdotnet2005
ID: 16662843
ERROR

The DataTableMapping.SourceTable is required to be unique, 'Table' already exists in the collection.


 DataGridView1.DataSource = da.tablemappings.add("Table", "badtable")
0
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 total points
ID: 16662862
my bad...typo

private ds as new dataset
private da as new oledbdataadapter

...

Dim StrConnection As String = "provider=microsoft.jet.oledb.4.0;" + "data source = C:\Documents and Settings\XMAN.SST_DOM\Desktop\ADDRESS\bad.mdb"
Dim Connection As New OleDbConnection(StrConnection)
Dim myCommand As New OleDbCommand("SELECT * FROM badtable", Connection)

da.selectcommand = myCommand
da.tablemappings.add("Table", "badtable")

Try
       da.Fill(ds)
       DataGridView1.DataSource = ds.tables("badtable")
Catch ex As OperationAbortedException
       MsgBox(ex.ToString)
End Try
0
 

Author Comment

by:VBdotnet2005
ID: 16663023
it works !!!  thank you very much  :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question