Question Get data from DataAdapter

elianeasmar

Well-known member
Joined
Oct 3, 2013
Messages
76
Programming Experience
Beginner
Hello. I need to get data into dataview (to loop through the rows in the dataview) from my dataset(after making some changing some records:insert, update, insert).
So i need an answer on these two questions:
Can i get data from dataadapter into dataview ? Or i have to get data from dataadapter into dataset then from dataset to dataview(If it's possible)

I have tried something like this:
 Dim strconnection As String = "Data Source=EASMAR-PC;Initial Catalog=Database Connection;Integrated Security=True;"
        _cn = New SqlConnection(strconnection)
        _cn.Open()
        cmd.Connection = _cn
        'Get the data from database into dataadapter
        _DataAdapter.SelectCommand = New SqlCommand("SELECT * FROM tblCustomer", _cn)
        'Fill the dataset from dataadapter
        _DataAdapter.Fill(_DataSet)
        Dim dt As New DataTable
        dt = _DataSet.Tables("tblCustomer")
        'dt contain nothing!!
        '_DataAdapter.Fill(dt)


        Debug.WriteLine(dt.Rows.Count.ToString)

And i am getting this error:
   Object reference not set to an instance of an object.

On this line:
Debug.WriteLine(dt.Rows.Count.ToString)
 
Last edited:
The problem is that you're trying to get a DataTable from the DataSet by name but you didn't give the DataTable a name when you created it in the first place. The name of the database table that you query and the name of the DataTable that you populate have no specific relationship. Read the documentation for the Fill method if you want to learn how to name the DataTable.

That said, what's the DataSet for in at all? If all you want is a Datatable then just create a DataTable in the first place and forget the DataSet.
 
Ok. Thank you. i mnaged to get the dataset content into dataview.
Dim dt As New DataTable
dt = _DataSet.Tables(0)
 
Ok. Thank you. i mnaged to get the dataset content into dataview.
Dim dt As New DataTable
dt = _DataSet.Tables(0)

Why create a new DataTable when you're just going to throw it away on the very next line and use an existing DataTable? This:
Dim dt As New DataTable
is shorthand for this:
Dim dt As DataTable = New DataTable
which is equivalent to this:
Dim dt As DataTable

dt = New DataTable
so your code is actually doing this:
Dim dt As DataTable

dt = New DataTable
dt = _DataSet.Tables(0)
I assume that you can see the redundant part.

As I said though, why create a DataSet at all if all you want is a DataTable? If what you want is a DataTable then why not just create a DataTable? If all you wanted was one Integer then would you create an Integer array to put it in? Of course you wouldn;t, so why would you create a DataSet just to put a single DataTable in?

That said, if you are going to have multiple DataTables, particularly if there will be DataRelations between them, a DataSet is a good idea and possibly essential. If not though, it makes no sense to use one.
 
Back
Top