Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 dataset's 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 dataset's 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

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

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

 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

 

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

Posted (edited)

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 :)

Edited by sony
Posted

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

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

 

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 ....

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

Posted

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

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

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

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

 

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...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...