Retrieving a single value from a dataset

arkoe

Member
Joined
Nov 8, 2005
Messages
5
Programming Experience
1-3
[Resolved]Retrieving a single value from a dataset/Hiding a Column in a Datagrid

I know it's easier to do from the datagrid, but I'm trying to to acquire the value of basically a single field in a predefined tabled in my dataset by it's coordinates within the certain table. Is there any way to do this? I haven't been able to find one.

Thanks.
 
Last edited:
myDataSet.Tables(tableIndex).Rows(rowIndex)(columnIndex)

OR

myDataSet.Tables(tableName).Rows(rowIndex)(columnName)

Note that the data in the DataGrid does not necessarily match what is in the DataSet/DataTable exactly because it may be sorted and/or filtered. The data displayed in the DataGrid actually corresponds to the DefaultView propertie(s) of the relevant DataTable(s), which also corresponds to the DataSet's DefaultViewManager property.
 
u can use something like:


dataAdapter.Fill(dataSet)

valueHolder = dataSet.Tables(0).Rows(rownum)(col)

where rownum will be the Row Number where the value is located & col is either the column number or column name. Also you can replace Tables(0) with Tables(tableName).
 
Ok well, that didn't work as I had hoped due to the dataset not following the datagrid when it was filtered, so now I'm simply trying to hide my autogenerated ID field by setting the field width to zero. Unfortunately when I do, I get the following error during runtime: "Object reference not set to an instance of an object."

Here is my code, it sends the error at the bolded line:

VB.NET:
Module Main
...
Public dsGrooming As New DataSet
...
End Module

    Sub AdminSuppliesFill()
        Connect()
        Try
            odaGrooming.SelectCommand = New OleDb.OleDbCommand
            With odaGrooming.SelectCommand
                .CommandType = CommandType.StoredProcedure
                .CommandText = "spAdminSupplies"
                .Connection = cnnGrooming
            End With
            dsGrooming.Clear()
            odaGrooming.Fill(dsGrooming, "AdminSupplies")


            myFormBarkandPurr.dgrAdminSupplies.DataSource = Nothing
            myFormBarkandPurr.dgrAdminSupplies.DataSource = dsGrooming.Tables("AdminSupplies")
[B]             myFormBarkandPurr.dgrAdminSupplies.TableStyles("AdminSupplies").MappingName = "Table"[/B]
            myFormBarkandPurr.dgrAdminSupplies.TableStyles("Table").GridColumnStyles(0).Width = 0
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        cnnGrooming.Close()
    End Sub

Module modDatabse
    Public Sub Connect()
        cnnGrooming.ConnectionString = "Provider=Microsoft.Jet.oledb.4.0; Data Source = 477.mdb"
    End Sub

End Module
Any ideas? Thanks again for the help.
 
Last edited:
I said that in my post. If you want the data that corresponds to a dataGrid then substitute "DefaultView" for "Rows" in the code I provided. You did ask for the coordinates within a table and not within a grid.
 
jmcilhinney said:
I said that in my post. If you want the data that corresponds to a dataGrid then substitute "DefaultView" for "Rows" in the code I provided. You did ask for the coordinates within a table and not within a grid.

I'm sorry, I meant to give you more acknowledgement in my last post for mentioning that. Now I'm just trying to figure out why I get that error when I get to that bolded line of code. Didn't mean to upset you, sorry if I did.
 
Put a breakpoint on that line and then test the objects referenced to see which one is Nothing, then work back to the point that you thought that object was being created. My guess would be that there is no item named "AdminSupplies" in the TableStyles collection, but if you use the debugger properly then you don't have to guess.
 
I'm sorry, but I still can't figure this out. How would I made sure that "AdminSupplies" is in the TableStyles collection? I don't know how to use the debugger well. Thanks again.
 
Wow, finally got it. I think my problem stemmed from the fact that I set the datasource of the datagrid to the dataset rather than binding it. Because I did it this way, the mapping name had to be set to the name of the table in the dataset that the datagrid was showing. Here's the code that ended up working for me if anyone else runs into this:

VB.NET:
Sub Load2()
        Try
            Dim AdminFrills As New DataGridTableStyle
            AdminFrills.MappingName = "AdminFrills"
            AdminFrillsFill()
            myFormBarkandPurr.dgrAdminFrills.TableStyles.Add(AdminFrills)
            myFormBarkandPurr.dgrAdminFrills.TableStyles("AdminFrills").GridColumnStyles(0).Width = 0
            myFormBarkandPurr.dgrAdminFrills.TableStyles("AdminFrills").GridColumnStyles(1).Width = 220
Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Sub AdminFrillsFill()
        Connect()
        Try
            odaGrooming.SelectCommand = New OleDb.OleDbCommand
            With odaGrooming.SelectCommand
                .CommandType = CommandType.StoredProcedure
                .CommandText = "spAdminFrills"
                .Connection = cnnGrooming
            End With
            dsGrooming.Clear()
            odaGrooming.Fill(dsGrooming, "AdminFrills")
            myFormBarkandPurr.dgrAdminFrills.DataSource = Nothing
            myFormBarkandPurr.dgrAdminFrills.DataSource = dsGrooming.Tables("AdminFrills").DefaultView

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        cnnGrooming.Close()
    End Sub

Load2() is called in my form's load event.
 
Back
Top