Help inserting Records

Discussion in 'ASP .Net' started by Brian Ciarcia, Mar 4, 2005.

  1. Can someone please take a look at this code and tell me what I am doing
    wrong???

    I am getting the following error:

    ----------------------------------------------------
    Object reference not set to an instance of an object.
    --------------------------------------------------
    at this line:

    ---------------------------------------------------------
    objNewRow = objTable.NewRow()
    ------------------------------------------------


    Dim FirstName As String = Session("FirstName")
    Dim LastName As String = Session("LastName")
    Dim DOB As String = Session("DOB")
    Dim ResponseDate As String = Now()
    Dim SponsorCode As String = Session("SponsorCode")


    'Dim counter As Integer
    ' Dim arr() As String
    ' Dim coll As NameValueCollection

    ' coll = Request.Form

    'arr = coll.AllKeys

    'For counter = 0 To arr.GetUpperBound(0)
    'Response.Write("Form: " & arr(counter) & "<br>")
    Dim answer As String
    Dim AnsStr
    For Each answer In Request.Form
    'references to answer return the name of the item
    If (answer = "__VIEWSTATE") Or (answer = "Button1") Then
    Else

    Dim strConnection As String = Myconnection
    Dim StrSQL As String = "Select ResponseID, ResponseDate,
    LastName, FirstName, DOB, SponsorCode, QuestionID, QResponse from
    EligibilityResponse"
    Dim objDataSet As New DataSet
    Dim objConnection As OleDbConnection
    Dim objAdapter As OleDbDataAdapter
    Dim objCommand As OleDbCommand
    Dim objBuilder As OleDbCommandBuilder

    objConnection = New OleDbConnection(strConnection)
    objAdapter = New OleDbDataAdapter(StrSQL, objConnection)
    objBuilder = New OleDbCommandBuilder(objAdapter)

    objAdapter.InsertCommand = objBuilder.GetInsertCommand()

    Dim objTable As DataTable
    Dim objNewRow As DataRow

    objTable = objDataSet.Tables("EligibilityResponse")
    objNewRow = objTable.NewRow()
    objNewRow("ResponseDate") = ResponseDate
    objNewRow("LastName") = LastName
    objNewRow("FirstName") = FirstName
    objNewRow("DOB") = DOB
    objNewRow("SponsorCode") = SponsorCode
    objNewRow("QuestionID") = answer
    objNewRow("QResponse") = Request.Form(answer)
    objTable.Rows.Add(objNewRow)


    AnsStr = AnsStr & "the answer to question " & answer & "
    is " & Request.Form(answer) & " " & FirstName & " " & LastName & " " &
    DOB & " " & ResponseDate & " " & SponsorCode & " <br>"
    End If
    Next
    Label1.Text = AnsStr

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Brian Ciarcia, Mar 4, 2005
    #1
    1. Advertising

  2. Brian Ciarcia

    Karl Seguin Guest

    objDataSet.Tables("EligibilityResponse")

    must not return an actual table..I don't see anywhere tha tyou (a) fill the
    dataset or/and (b) add a table to it and name it that...

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Brian Ciarcia" <> wrote in message
    news:%23Kk$...
    > Can someone please take a look at this code and tell me what I am doing
    > wrong???
    >
    > I am getting the following error:
    >
    > ----------------------------------------------------
    > Object reference not set to an instance of an object.
    > --------------------------------------------------
    > at this line:
    >
    > ---------------------------------------------------------
    > objNewRow = objTable.NewRow()
    > ------------------------------------------------
    >
    >
    > Dim FirstName As String = Session("FirstName")
    > Dim LastName As String = Session("LastName")
    > Dim DOB As String = Session("DOB")
    > Dim ResponseDate As String = Now()
    > Dim SponsorCode As String = Session("SponsorCode")
    >
    >
    > 'Dim counter As Integer
    > ' Dim arr() As String
    > ' Dim coll As NameValueCollection
    >
    > ' coll = Request.Form
    >
    > 'arr = coll.AllKeys
    >
    > 'For counter = 0 To arr.GetUpperBound(0)
    > 'Response.Write("Form: " & arr(counter) & "<br>")
    > Dim answer As String
    > Dim AnsStr
    > For Each answer In Request.Form
    > 'references to answer return the name of the item
    > If (answer = "__VIEWSTATE") Or (answer = "Button1") Then
    > Else
    >
    > Dim strConnection As String = Myconnection
    > Dim StrSQL As String = "Select ResponseID, ResponseDate,
    > LastName, FirstName, DOB, SponsorCode, QuestionID, QResponse from
    > EligibilityResponse"
    > Dim objDataSet As New DataSet
    > Dim objConnection As OleDbConnection
    > Dim objAdapter As OleDbDataAdapter
    > Dim objCommand As OleDbCommand
    > Dim objBuilder As OleDbCommandBuilder
    >
    > objConnection = New OleDbConnection(strConnection)
    > objAdapter = New OleDbDataAdapter(StrSQL, objConnection)
    > objBuilder = New OleDbCommandBuilder(objAdapter)
    >
    > objAdapter.InsertCommand = objBuilder.GetInsertCommand()
    >
    > Dim objTable As DataTable
    > Dim objNewRow As DataRow
    >
    > objTable = objDataSet.Tables("EligibilityResponse")
    > objNewRow = objTable.NewRow()
    > objNewRow("ResponseDate") = ResponseDate
    > objNewRow("LastName") = LastName
    > objNewRow("FirstName") = FirstName
    > objNewRow("DOB") = DOB
    > objNewRow("SponsorCode") = SponsorCode
    > objNewRow("QuestionID") = answer
    > objNewRow("QResponse") = Request.Form(answer)
    > objTable.Rows.Add(objNewRow)
    >
    >
    > AnsStr = AnsStr & "the answer to question " & answer & "
    > is " & Request.Form(answer) & " " & FirstName & " " & LastName & " " &
    > DOB & " " & ResponseDate & " " & SponsorCode & " <br>"
    > End If
    > Next
    > Label1.Text = AnsStr
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Karl Seguin, Mar 4, 2005
    #2
    1. Advertising

  3. Karl,

    Thanks for responding.. You are right.. I screwed up.. I was using some
    sample code to insert some data and realized that wasn't the right code
    I should be looking at.. I am all set now.. thanks alot..



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Brian Ciarcia, Mar 4, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. blenderdude

    Inserting Records with Bound Controls

    blenderdude, Jul 21, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    blenderdude
    Jul 21, 2003
  2. Muhammad Usman
    Replies:
    1
    Views:
    1,094
    Raja Saravanan
    Oct 16, 2003
  3. Bijoy Naick

    Inserting Records from another table

    Bijoy Naick, Jun 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    341
    Steve C. Orr [MVP, MCSD]
    Jun 30, 2004
  4. Chumley the Walrus
    Replies:
    1
    Views:
    377
    Mark Rae
    Aug 12, 2004
  5. Brian

    inserting multiple records.....

    Brian, Feb 22, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    479
    John Timney \(ASP.NET MVP\)
    Feb 24, 2005
Loading...

Share This Page