dataset datatable' srelationship

sony

Member
Joined
Oct 12, 2005
Messages
9
hi i am using vb.net and winforms i have a datagrid which to it i am binding a dataset which has two relational datatables...

when i bind each datasets table individually to the datagrid they show... fine.. its in my relationship i am getting the error message which is highlighted below.. thanks in advance..sonia


global variables in forms module
Dim filepath As String allows user to get the path of the textfile to be retrieved
Dim oDT As New DataTable Datatable to hold the datasets table which is being passsed from another page

Dim oDSParentChild As New DataSet the dataset which is holding the two relational datatables
Dim oDSMeanReport As New DataSet this is holding the dataset of the data being retrieved from teh textfile
Dim oDSQuestion As New DataSet this is the dataset which is holding the dataset from another page
my form load is passing a Dataset from another form

Code:
Private Sub frmGetMean_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oDSQuestion = varoDS
        oDT = oDSQuestion.Tables(0)
        oDT.TableName = "Questions"
    End Sub
Code:
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuGetMeanFile.Click

        OpenFileDialog1.Title = "Test"
        OpenFileDialog1.InitialDirectory = "I:\StudentSurveyData\SPSS Main Report"
        OpenFileDialog1.Filter = "txt files (*.txt)|*.txt"
        OpenFileDialog1.FilterIndex = 2

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            filepath = OpenFileDialog1.FileName()

            oDSMeanReport = delimitedDataSet(vbTab, filepath)
            Me.DataGrid1.DataSource = ParentChildDataset
        End If
    End Sub

this is a function to get data from a textfile and put in into a dataset
Code:
 Public Function delimitedDataSet(ByVal strdelimeter As String, ByVal strFilePath As String) As DataSet

        Dim oDS As New DataSet
        Dim oDT As New DataTable
        Dim oDR As DataRow
        Dim intcount As Int32
        Dim strFields As String
        Dim i As Int32

        DATASET NAME, NAMESPACE, AND TABLE NAME
        oDS.DataSetName = "test"
        oDS.Namespace = "test"
        oDS.Tables.Add("test")

        Dim oSR As New StreamReader(strFilePath)
        StreamReader()
        GO TO THE TOP OF THE FILE AND GET THE BEGIN
        oSR.BaseStream.Seek(0, SeekOrigin.Begin)

        ADD THE HEADER COLUMNS TO THE DATSET 


        For Each strFields In oSR.ReadLine().Split(strdelimeter)

            If strFields = "" Then
                strFields = i
                i = i + 1
            End If

            intcount = 0
            oDS.Tables(0).Columns.Add(strFields)
        Next

        oSR.BaseStream.Seek(0, SeekOrigin.Current)

        NOW ADD THE FIELDS
        oDT = oDS.Tables(0)
        While (oSR.Peek() > -1)
            oDR = oDT.NewRow()
            For Each strFields In oSR.ReadLine().Split(strdelimeter)
                If strFields = "" Then

                    strFields = "Quest"
                    oDR(intcount) = strFields
                    intcount = intcount + 1
                Else
                     If strFields <> "" Then
                    oDR(intcount) = strFields
                    intcount = intcount + 1
                     End If
                End If
            Next
            intcount = 0
            oDT.Rows.Add(oDR)

        End While
        Return oDS
    End Function

this procedure is creating a relationship between the two tables and it is adding them to the dataset

Code:
Public Function ParentChildDataset() As DataSet
add a parent/child relationship between the  two dataset
        oDSParentChild.Relations.Add( _
            "QuestionsAndMean", _
          oDSParentChild.Tables("test").Columns("0"), _
         oDSParentChild.Tables("Questions").Columns("QuestionId"))
[COLOR=Red]i get error here object is not set to an reference... can someone tell me why
 i have all of my dataset and my datatables with a NEW keyword[/COLOR]

        Return oDSParentChild
    End Function
 
Does the oDSParentChild dataset have any tables created in it at the point of error? Looking at your posted code it looks like it hasnt had any tables created in it so far.
 
still trying to solve t problem

no it doesnt have any tables in it at the moment... ah that does make alot of sense... i will add them .... so you think thats why i am getting this error...
thanks for your reply :)
 
Last edited by a moderator:
hi i have changed my code it looks like this now... but still i get the same error message...

i have one question.. cause i dont know if i am doing this right... i have one datatable being populated from a sql server database which has a primary key. i have another datatable being populated from a textfile. basically the first column of this textfile which has been inserted into a datatable has the same column values as the primary key of the other table which is being populated from the sql server table...

now i am adding them to the same dataset... well basically the sql server table which is being retrieved first.. i have added that in a global module because it needs to be used in another table as well..so i am using that dataset which is in the global module in my winform.. so i am passing a dataset with a datatable then adding to this dataset the datatable with is being filled by a textfile.... then i am trying to add a relation between these two datatables which are in the same dataset (so yeah the dataset is global) but i keep on getting the error message object not set to an instance of the object ....

i have changed my code from the previous time i posted it.. its as follows.. i have highlighted the function names to be more understand able.... thanks in advance to whom can help me out...

this procedure is allowing the user to retrieve the textfile and then passing it to the function which returns a datatable
Code:
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuGetMeanFile.Click
        OpenFileDialog1.Title = "Test"
        OpenFileDialog1.InitialDirectory = "I:\StudentSurveyData\SPSS Main Report"
        OpenFileDialog1.Filter = "txt files (*.txt)|*.txt"
        OpenFileDialog1.FilterIndex = 2

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            filepath = OpenFileDialog1.FileName()

                 oDSpub.Tables.Add([COLOR=DarkOrange]delimitedDataSet(vbTab, filepath)[/COLOR])


            Me.DataGrid1.DataSource = [COLOR=YellowGreen]ParentChildDataset()[/COLOR]         

        End If
    End Sub

Code:
Public Function [COLOR=DarkOrange]delimitedDataSet(ByVal strdelimeter As String, ByVal strFilePath As String)[/COLOR] As DataTable

        Dim oDS As New DataSet
        Dim oDT As New DataTable("test")
        Dim oDR As DataRow
        Dim intcount As Int32
        Dim strFields As String
        Dim i As Int32

        
        Dim oSR As New StreamReader(strFilePath)
        
        GO TO THE TOP OF THE FILE AND GET THE BEGIN
        oSR.BaseStream.Seek(0, SeekOrigin.Begin)

        ADD THE HEADER COLUMNS TO THE DATSET 

        Dim strFieldss As String
        For Each strFields In oSR.ReadLine().Split(strdelimeter)

            If strFields = "" Then
                strFields = i
                i = i + 1
            End If

            intcount = 0
            oDT.Columns.Add(strFields)
        Next

        oSR.BaseStream.Seek(0, SeekOrigin.Current)
        NOW ADD THE FIELDS
             While (oSR.Peek() > -1)
            oDR = oDT.NewRow()
            For Each strFields In oSR.ReadLine().Split(strdelimeter)
                If strFields = "" Then

                    strFields = "Quest"
                    oDR(intcount) = strFields
                    intcount = intcount + 1
                Else
                                      oDR(intcount) = strFields
                    intcount = intcount + 1
                                   End If
            Next
            intcount = 0
            oDT.Rows.Add(oDR)


        End While
        Return oDT
    End Function

this is the function which i am binding to my datagrid and i keep on the error message ....
Code:
Public Function [COLOR=YellowGreen]ParentChildDataset() [/COLOR] As DataSet
        Dim relation As New DataRelation( _
            "QuestionsAndMean", _
            oDSpub.Tables("Questions").Columns("QuestionId"), _
          oDSpub.Tables("test").Columns("0"))
[COLOR=Red]i get error here object is not set to an reference... can someone tell me why
 i have all of my dataset and my datatables with a NEW keyword[/COLOR]
        Return oDSpub
    End Function
 
Are you adding the data table to the dataset anywhere?

Also if you use [ vb ] [ /vb ] for the code blocks instead of the [ code ] [ /code ] it will colour the text for you anyway.
 
Hi thanks for your reply.. yes i do have them added one of the datatable is added below and this is in the form which has the datagrid

this is my global module which has the dataset
Code:
Imports System.Data
Imports System.Data.sqlclient
Imports System.IO
Imports System.text
Imports System.collections
Imports System.Xml

Module Global
    Dim connStr As New SqlConnection(Configuration.ConfigurationSettings.AppSettings("StudentSurvey"))
        Public oDSpub As New DataSet
    Public Function getQuestions() As DataTable
        Dim oDA As New SqlDataAdapter("Select * FROM Questions", connStr)

        Dim oDT As New DataTable
        oDA.Fill(oDSpub, "Questions")
        oDSpub.DataSetName = "Questions"
        oDT = oDSpub.Tables(0)
        Return oDT
    End Function
End Module


the code is below is calling this dataset and filling it with the datatable of the function
Code:
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuGetMeanFile.Click
        OpenFileDialog1.Title = "Test"
        OpenFileDialog1.InitialDirectory = "I:\StudentSurveyData\SPSS Main Report"
        OpenFileDialog1.Filter = "txt files (*.txt)|*.txt"
        OpenFileDialog1.FilterIndex = 2

        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then

            filepath = OpenFileDialog1.FileName()

                 oDSpub.Tables.Add(delimitedDataSet(vbTab, filepath))


            Me.DataGrid1.DataSource = ParentChildDataset()         

        End If
    End Sub

and this is my function


this is my data relation function
Code:
Public Function delimitedDataSet(ByVal strdelimeter As String, ByVal strFilePath As String) As DataTable

        Dim oDS As New DataSet
        Dim oDT As New DataTable("test")
        Dim oDR As DataRow
        Dim intcount As Int32
        Dim strFields As String
        Dim i As Int32
 
        Dim oSR As New StreamReader(strFilePath)
             GO TO THE TOP OF THE FILE AND GET THE BEGIN
        oSR.BaseStream.Seek(0, SeekOrigin.Begin)

        ADD THE HEADER COLUMNS TO THE DATSET 

        Dim strFieldss As String
        For Each strFields In oSR.ReadLine().Split(strdelimeter)

            If strFields = "" Then
                strFields = i
                i = i + 1
            End If

            intcount = 0
            oDT.Columns.Add(strFields)
        Next

        oSR.BaseStream.Seek(0, SeekOrigin.Current)
        NOW ADD THE FIELDS
             While (oSR.Peek() > -1)
            oDR = oDT.NewRow()
            For Each strFields In oSR.ReadLine().Split(strdelimeter)
                If strFields = "" Then

                    strFields = "Quest"
                    oDR(intcount) = strFields
                    intcount = intcount + 1
                Else
                                      oDR(intcount) = strFields
                    intcount = intcount + 1
                                   End If
            Next
            intcount = 0
            oDT.Rows.Add(oDR)


        End While
        Return oDT
    End Function

Code:
Public Function ParentChildDataset() As DataSet
        Dim relation As New DataRelation( _
            "QuestionsAndMean", _
            oDSpub.Tables("Questions").Columns("QuestionId"), _
          oDSpub.Tables("test").Columns("0"))

        Return oDSpub
    End Function

now i am getting an error column argument cannot be null. well i will have to sort that out at least i am not getting the old error : instance not set to an object...
 
Back
Top