Hi everyone, I'm having this problem with my code and cannot seem to get past it although it was working fine before.

The error is "An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object."

The code is:

Private Sub btneditstudent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneditstudent.Click

    Dim sqlx As OleDbCommand = New OleDbCommand
    Dim con As New OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source = Decisionmaths.mdb")

    Dim xyz As String

    xyz = Mid(lstboxstudents.SelectedItem, 1, 8)

    sqlx.CommandText = ("SELECT * FROM Student WHERE StudentNumber = " & xyz)

    sqlx.Connection = con
    con.Open()

    Dim rdr As OleDbDataReader = sqlx.ExecuteReader()

    If rdr.Read = True Then
        frmviewsdets.txtsnumber.Text = rdr("StudentNumber")
        frmviewsdets.txtfname.Text = rdr("SFirstName")
        frmviewsdets.txtsname.Text = rdr("SSurname")
        frmviewsdets.txttgroup.Text = rdr("TutorGroup")
        frmviewsdets.txtascore.Text = rdr("ALISScore")
        frmviewsdets.txtgcsemath.Text = rdr("GCSEMathsGrade")
        frmviewsdets.txtattend.Text = rdr("Attendance")
        frmviewsdets.txtclassid.Text = rdr("ClassID")
        frmviewsdets.txtpword.Text = rdr("SPassword")
    End If

    con.Close()

    frmviewsdets.btndeletesdets.Hide()
    frmviewsdets.btnaddsdets.Hide()
    frmviewsdets.btnviewsdets.Hide()
    frmviewsdets.Show()
    Me.Hide()

End Sub

EXTRA INFO IF NEEDED: I am trying to get information to be pulled out of a database and show in text boxes on another form (frmviewsdets). The error occurs when it gets to the line frmviewsdets.txtsnumber.Text = rdr("StudentNumber") and even when a breakpoint is applied there it just moves on to the next and so on.

I have tried to show frmviewsdets before I pull the data out of the database beut I get the same message. Any help would be appreciated, thank you!

what is this :

" & xyz


try this : '" & xyz & "'

or this : '" & lstboxstudents.SelectedItem & "'

Make sure that the xyz = Mid(lstboxstudents.SelectedItem, 1, 8) has atleast 9 characters in it....

Dim con As New OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source = Decisionmaths.mdb")
Dim xyz As String
xyz = Mid(lstboxstudents.SelectedItem, 1, 8)
Dim sqlx As OleDbCommand 
con.Open()
sqlx=New OleDbCommand("SELECT * FROM Student WHERE StudentNumber = '"+xyz+"'",con)
Dim rdr As OleDbDataReader = sqlx.ExecuteReader()
If rdr.Read = True Then
    frmviewsdets.txtsnumber.Text = rdr("StudentNumber")
    frmviewsdets.txtfname.Text = rdr("SFirstName")
    frmviewsdets.txtsname.Text = rdr("SSurname")
    frmviewsdets.txttgroup.Text = rdr("TutorGroup")
    frmviewsdets.txtascore.Text = rdr("ALISScore")
    frmviewsdets.txtgcsemath.Text = rdr("GCSEMathsGrade")
    frmviewsdets.txtattend.Text = rdr("Attendance")
    frmviewsdets.txtclassid.Text = rdr("ClassID")
    frmviewsdets.txtpword.Text = rdr("SPassword")
End If
rdr.Close()
con.Close()
frmviewsdets.btndeletesdets.Hide()
frmviewsdets.btnaddsdets.Hide()
frmviewsdets.btnviewsdets.Hide()
frmviewsdets.Show()
Me.Hide()

Put everything in a try catch block

To artemix22 xyz is used so that I can retrieve only the ID from the selected index in the listbox. I didas you advised me to and got an error saying "Data type mismatch in criteria expression." on the line
Dim rdr As OleDbDataReader = sqlx.ExecuteReader()

To poojavb I tried tosed but vb wouldn't allow me to and I was met with the blue line under the code.

If there is any field in your database (all others then varchar - string actually) you must provide this type.
I see you provide for example "xyz" parameter, which is (surely) an integer, you have to convert string to integer (thats why you got this kind of exception).
so like (if xyz is non a string)

"... WHERE StudentNumber = '" + Convert.ToInt32(xyz) + "'";

Okay I see where you're coming from Mitja, but this was not my original problem and seems to be working fine the way it was originallu. However my real issue is this error message "An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object." on the line:

frmviewsdets.txtsnumber.Text = rdr("StudentNumber")

Which is where I try to load the data pulled from the database into a text box on the frmviewsdets

so try this instead of

frmviewsdets.txtsnumber.Text = rdr("StudentNumber")

use this

frmviewsdets.txtsnumber.Text = xyz 'which is ur variable
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.