Why can't I get this bit of code in Microsoft Visual Basic 2005 Express Edition that uses SQL Databases to work? RRS feed

  • Question

  • I have been having many problems with trying to get this database to work. I am using Microsoft Visual Basic 2005 Express Edition. I am somewhat of a beginner programmer, at least in the line of Databases. I am still in the debugging stage of a sample program that I am using to try and learn how to use databases.

    The error I have found is that when ever I try and access some information, the program tells me that there is no row at a specific location, which is usually at row 0. I have tried using the "findby" method, but it still doesn't work. I have been having other problems as well. Here is a summary of what I have done and what the problem code looks like:

    The database I have made is one that records: Index of record, Name of user, Favourite Number of user, and Favourite Colour of user. That is the main table, and I have a second table that holds the possible different colours. (So the favourite Colour table also had an index and the string "colour", although it was technically called "nvarchar(50)"). I have records 0 to 10 on the Favourite Colour table already recorded, and the main table is empty. Note that where I said it records the "Favourite Colour", it is actually recording the index of the main table. I do have a connection between the two tables. Anyway, here is some of what I have:

    Public nfn As New NameFavNumDS

    Later, inside a subroutine, I have...

    For i As Integer = 0 To 10

    Dim arow As NameFavNumDS.ColourRow = nfn.Colour.FindByIndex(i)

    'If arow.colour.Trim = favcol.Trim Then favcolnum = i : found = True : Exit For

    'If nfn.Colour.FindByIndex(i).colour.Trim = favcol.Trim Then favcolnum = i : found = True : Exit For

    MsgBox("Colour name at index " + Str(i) + ": " + nfn.Colour.FindByIndex(i).colour)

    Next i


    The commented out portions were the ones I originally tried, but even the "MsgBox" line came up with an error. The error this time is: "NullReferenceException"  (and that it "was unhandled"). I am supposed to put a "new" in the declaration of "arow" and the rest will work?

    I had also tried (but deleted) the following line:

    MsgBox("Colour name at index " + Str(i) + ": " + nfn.Colour(i).colour)

    It came up with the error "IndexOutOfRangeException". I tried

    For i As Integer = 1 To 10

    That doesn't work either. Still out of range.


    So I thought if I took out the "New" from the orginal declaration, so I had

    Public nfn As NameFavNumDS

    It would work, only there were errors since it was referencing a null object again.


    Here's a little history of what I had done before this problem:

    First, I had both "index" 's set as not only the primary key but the identity specification as well. I have this one tutorial that said to do that. When I tried to run my program, there were those outOfRange problems, so I figured if I allowed the Database to make up it's own names for the rows that I could use the "index" as my own name. Then, I could simply use the "nfn.Colour.findbyIndex(theIndex)". I tried this, and so far I still don't know if that worked because when I did that to the Colour table I never got so far as to test that out. SO, at current, both of the "index" in the 2 tables are the primary key, but not the identity specification.


    What am I doing wrong, how can it be fixed?

    Monday, July 9, 2007 11:42 PM

All replies