An error occurred creating the form question

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 4841 Views - Last Post: 15 April 2012 - 06:55 AM Rate Topic: -----

#1 ThatsSoShaneka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-April 12

An error occurred creating the form question

Posted 13 April 2012 - 04:39 PM

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!


Is This A Good Question/Topic? 0
  • +

Replies To: An error occurred creating the form question

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 576
  • View blog
  • Posts: 2,730
  • Joined: 24-July 11

Re: An error occurred creating the form question

Posted 13 April 2012 - 05:01 PM

Potential causes: not properly instantiated objects within the form. The form has been renamed but the compiler hasn't realized. Have you by any chance had a play around by the code in the formname.Designer code?
Was This Post Helpful? 0
  • +
  • -

#3 ThatsSoShaneka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-April 12

Re: An error occurred creating the form question

Posted 13 April 2012 - 05:13 PM

Thank you for your fast reply!!

But to be honest I'm quite new to vb and I'm not even sure how to view to the designer code. I've seen it before when I've had other errors but I don't know how to access it myself.

Was This Post Helpful? 0
  • +
  • -

#4 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 131
  • Joined: 21-January 12

Re: An error occurred creating the form question

Posted 13 April 2012 - 07:04 PM

dimitriv.. can we use ?

.. WHERE StudentNumber = " & xyz  ???



or like this :
.. WHERE StudentNumber = '" & xyz & "'  ???


Was This Post Helpful? 0
  • +
  • -

#5 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 576
  • View blog
  • Posts: 2,730
  • Joined: 24-July 11

Re: An error occurred creating the form question

Posted 13 April 2012 - 07:15 PM

May need the single quotes. Good catch!
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: An error occurred creating the form question

Posted 13 April 2012 - 07:19 PM

You should be using Parameterised Queries instead of string building, otherwise you are opening yourself to SQL injection attacks.
Was This Post Helpful? 0
  • +
  • -

#7 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 131
  • Joined: 21-January 12

Re: An error occurred creating the form question

Posted 13 April 2012 - 07:29 PM

ThatsSoShaneka, try to change this part :

... WHERE StudentNumber = " & xyz)



to :

... WHERE StudentNumber = '" & xyz & "';")


Was This Post Helpful? 0
  • +
  • -

#8 ThatsSoShaneka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-April 12

Re: An error occurred creating the form question

Posted 13 April 2012 - 09:12 PM

View Postartemix22, on 13 April 2012 - 07:29 PM, said:

ThatsSoShaneka, try to change this part :

... WHERE StudentNumber = " & xyz)



to :

... WHERE StudentNumber = '" & xyz & "';")



Okay so I did that and now on this line of my code
Dim rdr As OleDbDataReader = sqlx.ExecuteReader()


I'm getting an error saying "Data type mismatch in criteria expression."

Was This Post Helpful? 0
  • +
  • -

#9 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 576
  • View blog
  • Posts: 2,730
  • Joined: 24-July 11

Re: An error occurred creating the form question

Posted 13 April 2012 - 10:56 PM

OK, that must not be the problem. Can you think of a time where it did work (the form)? What have you changed since then?
What's the code for the form itself?
Was This Post Helpful? 0
  • +
  • -

#10 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 131
  • Joined: 21-January 12

Re: An error occurred creating the form question

Posted 14 April 2012 - 03:05 AM

View PostThatsSoShaneka, on 13 April 2012 - 09:12 PM, said:

View Postartemix22, on 13 April 2012 - 07:29 PM, said:

ThatsSoShaneka, try to change this part :

... WHERE StudentNumber = " & xyz)



to :

... WHERE StudentNumber = '" & xyz & "';")



Okay so I did that and now on this line of my code
Dim rdr As OleDbDataReader = sqlx.ExecuteReader()


I'm getting an error saying "Data type mismatch in criteria expression."



what about this :

... WHERE StudentNumber = " & xyz & ";")



you assign StudentNumber to integer or something in database, the single quotes (') will make it string, so remove the (')
Was This Post Helpful? 0
  • +
  • -

#11 artemix22  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 131
  • Joined: 21-January 12

Re: An error occurred creating the form question

Posted 14 April 2012 - 03:23 AM

may i see your lstboxstudents where hold your StudentNumber?

check this thread :

http://www.dreaminco...ria-expression/

This post has been edited by artemix22: 14 April 2012 - 03:27 AM

Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1538
  • View blog
  • Posts: 4,938
  • Joined: 25-September 09

Re: An error occurred creating the form question

Posted 14 April 2012 - 03:50 AM

So you're only getting this error when you try to show the other form?

Can we see the startup code for that form?
Was This Post Helpful? 0
  • +
  • -

#13 ThatsSoShaneka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-April 12

Re: An error occurred creating the form question

Posted 14 April 2012 - 11:15 AM

View Postartemix22, on 14 April 2012 - 03:23 AM, said:

may i see your lstboxstudents where hold your StudentNumber?

check this thread :

http://www.dreaminco...ria-expression/


As the form loads I get it to display the StudentNumber, FirstName and Surname of the students from the database into the listbox.

    Private Sub frmstudentdets_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

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

        sqlx.CommandText = ("SELECT StudentNumber, SFirstName, SSurname FROM Student")

        sqlx.Connection = con
        con.Open()

        Dim rdr As OleDbDataReader = sqlx.ExecuteReader()

        Do While rdr.Read() = True
            lstboxstudents.Items.Add(rdr.Item("StudentNumber") & " - " & (rdr.Item("SFirstName") & " " & (rdr.Item("SSurname"))))
        Loop

        con.Close()

    End Sub


And thanks I'll have a look at that and see if I can solve the other issue later.
Was This Post Helpful? 0
  • +
  • -

#14 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: An error occurred creating the form question

Posted 14 April 2012 - 12:16 PM

I got lost...if you still encounter the first error where do you instantiate frmviewsdets form? It is a global variable initialized on the constructor?
If you don't have the error from the first post anymore, what is the new issue?
Was This Post Helpful? 0
  • +
  • -

#15 ThatsSoShaneka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 13-April 12

Re: An error occurred creating the form question

Posted 14 April 2012 - 12:48 PM

View PostCharlieMay, on 14 April 2012 - 03:50 AM, said:

So you're only getting this error when you try to show the other form?

Can we see the startup code for that form?


There isn't really any start up code on that form, it is all generated from the previous form.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2