none
Error: 'Object reference not set to an instance of an object. RRS feed

  • Question

  • Please Look at:

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/populating-a-dataset-from-a-dataadapter

    Using connection As OleDbConnection = New OleDbConnection( _  
      "Provider=MSDataShape;Data Provider=SQLOLEDB;" & _  
      "Data Source=(local);Integrated " & _  
      "Security=SSPI;Initial Catalog=northwind")  
      
    Dim adapter As OleDbDataAdapter = New OleDbDataAdapter( _  
      "SHAPE {SELECT CustomerID, CompanyName FROM Customers} " & _  
      "APPEND ({SELECT CustomerID, OrderID FROM Orders} AS Orders " & _  
      "RELATE CustomerID TO CustomerID)", connection)  
      
    Dim customers As DataSet = New DataSet()  
      
    adapter.Fill(customers, "Customers")  
    End Using  
    

    When the Fill operation is complete, the DataSet contains two tables: Customers and CustomersOrders, where CustomersOrders represents the chaptered column. An additional column named Orders is added to the Customers table, and an additional column named CustomersOrders is added to the CustomersOrders table. The Orders column in the Customers table is set to auto-increment. A DataRelationCustomersOrders, is created by using the columns that were added to the tables with Customers as the parent table. The following tables show some sample results.

    i Tested, it is working fine

    Only i didnt found the way how to display the columns of the cheild table

    i treid 

    Dataset.Tables("CheildTable").Rows(0).Item("Column").ToString

    i got an error 

     'Object reference not set to an instance of an object.

     

    Sunday, October 6, 2019 6:18 PM

All replies

  • Dataset.Tables("CheildTable").Rows(0).Item("Column").ToString

    How to you know there are any rows in the table? I would think you would check if Table.Rows.Count > 0. If count > 0 then there are rows in the table and the above statement would work otherwise you're going to get an exception thrown if you try to address a row in the table and there is no rows in the table.

    Sunday, October 6, 2019 6:50 PM
  • thanks 

    i check this

    it dont help

    Sunday, October 6, 2019 7:15 PM
  • thanks 

    i check this

    it dont help

    Then how do you know that a column in the table row  has no data and is DBNull. It means that object instance cannot be created if the value of the data in DBNull. 

    Maybe, you need to check the column data for DBNull before you try to access data in the column.

    Sunday, October 6, 2019 7:28 PM
  • i can check by highlighting the string, and then check it in SQL

    i seee if it is empty or not

    did you check

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/populating-a-dataset-from-a-dataadapter

    this is not working like a reguler conection

    even a conection to sql is not posible only with a Ole conection 

    and the dataset is not working llike a reguler dataset

    Please check this and see

    the OleDbDataAdapter conects to a Shape of many tables

    and the fill, is only to 1 table

    i test it     and it is giving a error  ": 'Column 'charge' does not belong to table 'Charges' 

    even the the Column  is there

    thanks in advence 

     

    Sunday, October 6, 2019 7:42 PM
  • Here is what I get, note my CustomerID is CustomerIdentifier in my query and the connection string is a different server and catalog.

    Public Sub Demo()
    	Using connection As New OleDbConnection("Provider=MSDataShape;Data Provider=SQLOLEDB;Data Source=KARENS-PC;Integrated Security=SSPI;Initial Catalog=NorthWindAzure3")
    		Dim adapter As New OleDbDataAdapter("SHAPE {SELECT CustomerIdentifier, CompanyName FROM Customers} APPEND ({SELECT CustomerIdentifier, OrderID FROM Orders} AS Orders " & "RELATE CustomerIdentifier TO CustomerIdentifier)", connection)
    
    		Dim customers As New DataSet()
    		adapter.Fill(customers, "Customers")
    
    		For Each table As DataTable In customers.Tables
    			Console.WriteLine(table.TableName)
    			For Each column As DataColumn In table.Columns
    				Console.WriteLine($"{column.ColumnName,35} - Type: {column.DataType}")
    			Next
    
    			Console.WriteLine()
    		Next
    	End Using
    End Sub
    

    Results

    Customers
                     CustomerIdentifier - Type: System.Int32
                            CompanyName - Type: System.String
                                 Orders - Type: System.Int32
    
    CustomersOrders
                     CustomerIdentifier - Type: System.Int32
                                OrderID - Type: System.Int32
                        CustomersOrders - Type: System.Int32

    Quick and dirty method to peek at data

    Public Sub Demo()
    	Using connection As New OleDbConnection("Provider=MSDataShape;Data Provider=SQLOLEDB;Data Source=KARENS-PC;Integrated Security=SSPI;Initial Catalog=NorthWindAzure3")
    		Dim adapter As New OleDbDataAdapter("SHAPE {SELECT CustomerIdentifier, CompanyName FROM Customers} APPEND ({SELECT CustomerIdentifier, OrderID FROM Orders} AS Orders " & "RELATE CustomerIdentifier TO CustomerIdentifier)", connection)
    
    		Dim customers As New DataSet()
    		adapter.Fill(customers, "Customers")
    
    		For Each table As DataTable In customers.Tables
    			Console.WriteLine(table.TableName)
    			For Each row As DataRow In table.Rows
    				Console.WriteLine($"   {String.Join(",", row.ItemArray)}")
    			Next
    
    
    			Console.WriteLine()
    		Next
    	End Using
    End Sub
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, October 6, 2019 7:43 PM
    Moderator
  • Hi,

    Do you resolve the issue? If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 9, 2019 2:57 AM
    Moderator