Getting data from a database using a primary key

Hello. I am having trouble with a program.

I want to be able to get some data from my database table, given the value of the primary key. This is what I have so far:

projectName = DbDataSet.Projects.FindByid(projectId).name

where projectName is a String, and DbDataSet is my dataset. The variable projectId is the ID of the row I want to extract the data from, but every time I run my code I get a NullReferenceException.

I know that the row with the primary key value of projectId exists because I can see it when I view through server explorer.

Any help would be fantastic

Bryan St. Amour




Answer this question

Getting data from a database using a primary key

  • Ray_GTI-R

    You have several objects on a single line - any one of them could throw that error (for example, what if the 'find' cannot find an object, and yet your code is assuming it's found and trying to access a property of that object).



  • ptjhuang

    Here is the entire function.

    Private Sub WhatsThisToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WhatsThisToolStripMenuItem.Click
    If dgvSchedule.SelectedRows.Count > 0 Then
    'Get the values we need to pass into the what's this form.
    Dim name As String = Convert.ToString(dgvSchedule.SelectedRows(0).Cells(SCHEDULE_TEXT).Value)
    Dim projectId As Integer = Convert.ToInt32(dgvSchedule.SelectedRows(0).Cells(SCHEDULE_PROJECT_ID).Value)

    Dim projectName As String = ""


    'Check to make sure that the project isnt a common block, because if it is the database will throw an
    'error when we search it for a row id of -1. :)
    If projectId <> -1 Then

    projectName = DbDataSet.Projects.FindByid(projectId).name

    End If

    Dim frm As New frmWhatsThis()
    frm.pName = name
    frm.Show()
    Else
    MessageBox.Show("No row selected.")
    End If
    End Sub

    I stepped through the function with the debugger and all of the variables are working okay. It's just when I hit the projectName = DbDataSet.Projects.FindByid(projectId).name line it gives me the exception.



  • Polican

    Is this ASP.Net, or Visual Basic.Net
  • Aron Kolozs

    DbDataSet is my dataset. It was created when I created my datasource through the editor.

  • GavinRitchie

    Sorry, didn't say this earlier but can you also post the full message of the exception Is DbDataSet declared at some higher level, and set equal to something
  • Siri Vellanki - MSFT

    You need to post the code where you declare the various objects and variables.  The NullReferenceException means that something isn't initialized, but it's tough to tell what since there is no context for this code.  Also, are you using VB.Net 2003 or VB.Net 2005
  • Guilherme Labigalini

    Is DBDataSet some type of custom class I don't see a Project property for the DataSet class in VB (or a FindByID, for that matter). I may be missing something, or we may be on different versions of Visual Studio.

    In a regular dataset you could find an item using:

    SomeString = DBDataSet.Tables("Project").Rows.Find(ProjectID).Item("ProjectName")


  • Bugsflowers

    the full error message is: Object reference not set to an instance of an object.

    And yes, DbDataSet is declared at a higher level and is set.

    Thank you for all of your help



  • Genyus

    What type of database are you using
  • GMohr

    This is visual basic . net, and I am using visuall studio 2005

  • Getting data from a database using a primary key