-
Jan 5th, 2008, 08:13 AM
#1
Thread Starter
New Member
displaying data from csv sql query in datagrid
Hi,
I'm using VB 2008 Express and ive found some code that im trying to play with to run queries on a csv file, but im getting the error: Error 1 Reference to a non-shared member requires an object reference. C:\Documents and Settings\whe103247\My Documents\Visual Studio 2008\Projects\sql test\sql test\Form1.vb 20 13 sql test
I have form1.vb with a datagridview called DataGridView1 and im using this code below
Code:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Public Class Form1
Inherits System.Windows.Forms.Form
Dim objDataset1 As DataSet()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\;Extended Properties=Text;"
Dim objConn As New OleDbConnection(sConnectionString)
objConn.Open()
Dim objCmdSelect As New OleDbCommand("SELECT * FROM test.csv", objConn)
Dim objAdapter1 As New OleDbDataAdapter()
objAdapter1.SelectCommand = objCmdSelect
Dim objDataset1 As New DataSet()
objAdapter1.Fill(objDataset1, "Test")
DataGridView1 = objDataset1.Tables(0).DefaultView
objConn.Close()
End Sub
End Class
End Class
DataGridView1 = objDataset1.Tables(0).DefaultView
is the line causing errors.
Have you any idea how i can resolve this issue.
Regards
Liam
-
Jan 5th, 2008, 09:09 PM
#2
Re: displaying data from csv sql query in datagrid
First of all, why are you declaring the objDataset1 variable at the class level and then again at the local level? Do one or the other, not both.
As to your issue, DataGridView1 is a member variable of your form that refers to a DataGridView object. Its type is DataGridView. objDataset1.Tables(0).DefaultView returns a DataView object. Why are you trying to assign a DataView object to a variable of type DataGridView for a start? Even if you could do that, which you can't, you would then not be able to refer to your DataGridView object via that variable any more.
I assume what you're trying to do is to bind the DataTable to the grid in order to display the data it contains. In that case you need to assign to the DataSource property of the grid:
vb.net Code:
DataGridView1.DataSource = objDataset1.Tables(0).DefaultView
That said, there's no point getting the DefaultView and binding it when binding the DataTable itself will display the contents of the DefaultView anyway:
vb.net Code:
DataGridView1.DataSource = objDataset1.Tables(0)
That said, you should bind your data to your controls via BindingSources in most cases. It makes various aspects of data-binding easier. Add a BindingSource to your form from the Toolbox and then do this:
vb.net Code:
BindingSource1.DataSource = objDataset1.Tables(0) DataGridView1.DataSource = BindingSource1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|