locked
TableMappings not adding data to second table????? RRS feed

  • Question

  • User-2010267424 posted

     HI Here is my code: 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                Dim strConn As String = "server=WORKER;Initial Catalog=eSurvey_DotNet;User ID=WorkerAdmin; Password=WorkerAdmin"
                Dim strSql As String = "SELECT InspectionID, SurveyDate FROM " + " eSurv_Inspections; " + "SELECT ViolationID, InspectionID " + " FROM eSurv_Violations;"
                Dim conn As New SqlConnection(strConn)
                Dim da As New SqlDataAdapter(strSql, conn)
                da.TableMappings.Add("Inspections1", "eSurv_Violations")
                Dim _ds = New DataSet()
                da.Fill(_ds, "eSurv_Inspections")
                _ds.Relations.Add("Inspection_Violation", _ds.Tables("eSurv_Inspections").Columns("InspectionID"), _ds.Tables("eSurv_Violations").Columns("InspectionID"))
                _ds.Relations(0).Nested = True
                ListView1.DataSource = _ds.Tables("eSurv_Inspections")
                ListView1.DataBind()
            End If
        End Sub

     

    When I run the code it stops on this line:

    _ds.Relations.Add("Inspection_Violation", _ds.Tables("eSurv_Inspections").Columns("InspectionID"), _ds.Tables("eSurv_Violations").Columns("InspectionID"))

    and give me the error:

      System.NullReferenceException was unhandled by user code
      Message="Object reference not set to an instance of an object."
      Source="App_Web_s_bv-mpk"
      StackTrace:
           at List.Page_Load(Object sender, EventArgs e) in C:\inetpub\wwwroot\eSurveyDotNet\List.aspx.vb:line 18
           at System.Web.UI.Control.OnLoad(EventArgs e)
           at System.Web.UI.Control.LoadRecursive()
           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    When I looked more closely at the code I found that  

    _ds.Tables("eSurv_Inspections").Columns("InspectionID")

    was being filled with data (Did "_ds.Tables("eSurv_Inspections").Columns("InspectionID").Table(0).Item(0).ToString" in the immediate and got back the correct data.)

    But when I tried the same thing with

    _ds.Tables("eSurv_Violations").Columns("InspectionID")

    I get the error "Referednced object has a value of Nothing"

     

    Please help I am at a total loss as to why this is happening.

    Thank You

    MutlyP

    Saturday, October 23, 2010 8:32 PM

Answers

  • User-2010267424 posted

    Hi ALL

    I figured out the problem. When adding the tablemapping the first property is the source table. I had:

    da.TableMappings.Add("Inspections1", "eSurv_Violations")

    But Inspections1 is not the source table name. It should be

    da.TableMappings.Add("eSurv_Inspections1", "eSurv_Violations")

    That was it.

    Thanks for nothing I guess Tongue out

    Just kidding

    MutlyP
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 24, 2010 1:52 AM