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

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
Aron Kolozs
GavinRitchie
Siri Vellanki - MSFT
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
GMohr