Results 1 to 4 of 4

Thread: Object Reference error on a counter?!?!

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Object Reference error on a counter?!?!



    Under normal circumstances I am not TOO bad at trying to figure errors out. However, I can't seem to understand why I am getting the age ol' "Object reference not set to an instance of an object error" in this lil bit of code.

    This is where I define my 'intTotalRecords'
    Code:
            Connection.Open()
            Command.CommandText = "SELECT * FROM CompanyTable"
            Command.Connection = Connection
            DAdapter.SelectCommand = Command
            DAdapter.Fill(Dset, "MyTable")
            intTotalRecords = Dset.Tables("MyTable").Rows.Count
            Connection.Close()
    However, I am getting the error here where I am attempting to set the value of intCount to the value that is held in total records + 1. The above block of code runs first to get the count of records. It may return 0 if no records exist and THAT I understand but I am not understanding the object reference in this instance!!!
    Code:
           intCount = intTotalRecords + 1
            CompRec(intCount).CompID = intCount
            CompRec(intCount).Street = frmNew.txtStreet.Text
            CompRec(intCount).Phone = CInt(frmNew.txtPhone.Text)
            CompRec(intCount).AccntNum = frmNew.txtAccntNum.Text
            CompRec(intCount).Balance = CDec(frmNew.txtCurrBal.Text)
            CompRec(intCount).DateDue = CDate(frmNew.txtDateDue.Text)
            CompRec(intCount).WebUrl = frmNew.txtUrl.Text
            CompRec(intCount).State = frmNew.txtState.Text
            CompRec(intCount).Zip = CInt(frmNew.txtZip.Text)
            CompRec(intCount).CompName = frmNew.txtCompName.Text
    Hope everyone is looking forward to a Merry Christmas.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  2. #2

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: Object Reference error on a counter?!?!

    Ok I have double checked the variable scope again and everything is currently public. I know even if no records exist within the table that rows.count will return 0 but I have dealt with that. I know that the Array of Structs that I am using is declared AND Instantiated correctly. This is really just chapping my ass.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  3. #3
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Object Reference error on a counter?!?!

    There are few things to be noted here. The DAdapter.Fill method will open the connection and closes the connection during fill. There is no need to explicitly open and close the connection.

    And for the enumurating the rows you could like

    For intCount as Integer = 0 to Dset.tables("MyTable").Count -1 '-1 as the index will start from 0

    Next IntCount
    Please mark you thread resolved using the Thread Tools as shown

  4. #4
    New Member
    Join Date
    Dec 2009
    Posts
    12

    Re: Object Reference error on a counter?!?!

    0.o too complex for me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width