I managed to resolve my problem
I just made some changes to the FillAbsentNumbers() and got rid of the
FillNamesNeeded()
Private Sub FillNumberAbsent()
Dim i As Integer
For i = 1 To objDS.Tables("NEWB").Rows.Count
strNumber = objDS.Tables("NEWB").Rows(i - 1).Item
("Absent")
lstNumberAbsent.Items.Add(strNumber)
StudentName = objDS.Tables("NEWB").Rows(i - 1).Item
("Student")
lstStudentNames.Items.Add(StudentName)
ClassName = objDS.Tables("NEWB").Rows(i - 1).Item("Class
Name")
lstClassNames.Items.Add(ClassName)
Next
End Sub
So I was actually getting values for StudentName and ClassName - the
listboxes I populate with the names are on the form but invisible to
the user.
On Feb 25, 4:56 pm, Cerebrus <[email protected]> wrote:
> Whenever you get Object Reference errors (NullReferenceException -
> NRE), the best thing to do is to break down your statement on the
> basis of nullability:
>
> StudentName = objRow2.Item("Student")
>
> 1. Can StudentName be null ? No, it is being assigned to.
> 2. Can objRow be null? Yes, if no matching row was found. This is the
> possible cause of the error. You always need to check this case.
> 3. Can Item("Student") be null ? It may be possible that the column
> student is not present in the retrieved dataset. But in that case, the
> likely exception thrown would be an ArgumentException, not an NRE.
>
> So, in this way, you can easily isolate the root cause of the error.
> (Cause #2)
>
> On Feb 25, 3:27 pm, Laura <[email protected]> wrote:
>
>
>
> > I got rid of the MissingSchemaAction part of the code and left the
> > FillSchema.
> > Then i set the Primary Key by going:
>
> > objAbsentNumberDA.FillSchema(objDS, SchemaType.Source, "NEWB")
> > objAbsentNumberDA.Fill(objDS, "NEWB")
> > objDS.Tables("NEWB").PrimaryKey = New DataColumn()
> > {objDS.Tables("NEWB").Columns("StudentID")}
>
> > But then I keep getting the "Object reference not set to an instance
> > of an object." error in relation to the following line of code:
> > StudentName = objRow2.Item("Student") - from FillNamesNeeded()
>
> > I've been messing with this all morning - any ideas as to what's
> > wrong???
>
> > Thanks
>
> > On Feb 25, 7:51 am, Cerebrus <[email protected]> wrote:
>
> > > Some OleDbDataAdapter datasources do not return Primary Key
> > > information along with the returned data. Since you are using an mdb
> > > (MS Access), it should return the Primary key when you set the
> > > MissingSchemaAction or call the FillSchema method explicitly.
> > > Therefore, I am unsure as to why this doesn't work, but there may be a
> > > couple of things you can look into:
>
> > > 1. Try using only one of the two calls - set the MissingSchemaAction
> > > OR call the FillSchema method. Not both.
> > > 2. Confirm that your Access database has a column configured as the
> > > Primary key within the table "NEWB".
> > > 3. If nothing works, try setting the Primary key explicitly to the
> > > appropriate column in the following way:
>
> > > ---
> > > Dim newB as DataTable = objDS.Tables("NEWB")
> > > newB.PrimaryKey = new DataColumn() { newB.Columns("MyPrimaryColumn") }
> > > ---
>
> > > On Feb 23, 11:29 pm, Laura <[email protected]> wrote:
>
> > > > Hi,
>
> > > > I have a database query that I am reading data from into a form. I can
> > > > read one column into a listbox without any problems.
> > > > Where I get into difficulty is when I try to use the data read into
> > > > the listbox to get the related rows.
>
> > > > I keep getting the following error:
>
> > > > "Missing PrimaryKey Was Unhanled Exception"
> > > > "Table doesn't have a primary key"
>
> > > > The query has four fields and one of them is a primary key. Is reading
> > > > from queries very different to reading from tables??
>
> > > > Here is the code I have;
>
> > > > Dim objConnection As New OleDb.OleDbConnection( _
> > > > "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= FYP.mdb")
> > > > Dim objAbsentNumberDA As New OleDb.OleDbDataAdapter("Select * from
> > > > NEWB", objConnection)
> > > > Dim objAbsentNumberCB As New OleDb.OleDbCommandBuilder
> > > > (objAbsentNumberDA)
> > > > Dim strNumber As String
> > > > Dim StudentName As String
> > > > Dim ClassName As String
>
> > > > then under the form load;
>
> > > > objAbsentNumberDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
> > > > objDS.Clear()
> > > > objAbsentNumberDA.FillSchema(objDS, SchemaType.Source, "NEWB")
> > > > objAbsentNumberDA.Fill(objDS, "NEWB")
>
> > > > lstNumberAbsent.Items.Clear()
> > > > FillNumberAbsent()
> > > > FillNamesNeeded()
>
> > > > Private Sub FillNumberAbsent()
> > > > Dim i As Integer
> > > > For i = 1 To objDS.Tables("NEWB").Rows.Count
> > > > strNumber = objDS.Tables("NEWB").Rows(i - 1).Item
> > > > ("Absent")
> > > > lstNumberAbsent.Items.Add(strNumber)
> > > > Next
> > > > End Sub
>
> > > > Private Sub FillNamesNeeded()
> > > > Dim objRow2 As DataRow
> > > > objRow2 = objDS.Tables("NEWB").Rows.Find(strNumber)
> > > > StudentName = objRow2.Item("Student")
>
> > > > FillNumberAbsent() works fine. It's in FillNamesNeeded() that I get
> > > > the error at the line: objRow2 = objDS.Tables("NEWB").Rows.Find
> > > > (strNumber)
>
> > > > Am I doing something wrong, or has it something to do with the fact
> > > > that I'm using queries?
>
> > > > Thanks- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -