-1

I am having a problem when i am trying to put this data into my database I'm using Vstudio 2013 and MS Access as my database

my problem is everytime i click add to add the data in my database this error always popping object reference not set to an instance of the object. even i declared the

Error Image please click

Here's my Add button Code

Dim cn As OleDb.OleDbConnection  
Dim cmd As OleDb.OleDbCommand

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

        Try


        If cn.State = ConnectionState.Open Then
            cn.Close()
        End If
        cn.Open()
        cmd.Connection = cn
            cmd.CommandText = "INSERT INTO gradess ( StudentNo,StudentName,StudentSection,SubjectNo1,SubjectNo2,SubjectNo3,SubjectNo4,SubjectNo5,SubjectNo6,SubjectNo7,SubjectNo8,TotalAverage) " & "Values('" & txtStudentNo.Text & "','" & lblName.Text & "','" & lblSection.Text & "','" & txtSubject1.Text & "','" & txtSubject2.Text & "','" & txtSubject3.Text & "','" & txtSubject4.Text & "','" & txtSubject5.Text & "','" & txtSubject6.Text & "','" & txtSubject7.Text & "','" & txtSubject8.Text & "','" & lblTotalAverage.Text & "')"

        cmd.ExecuteNonQuery()

        refreshlist()
        disablebutton()

        MsgBox("Successfully Added!!", vbInformation, "Successful")

            clear()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub
Erik A
  • 31,639
  • 12
  • 42
  • 67
  • When the error comes up type Cntrl Break. The find which instruction is failing and hover over the variables to see which one is null. You should test if cn is null before you test cn.State. cn = null indicates the connection is closed. – jdweng May 13 '18 at 06:39
  • Your declaration should be : Dim cn As New OleDb.OleDbConnection Dim cmd As New OleDb.OleDbCommand – F0r3v3r-A-N00b May 13 '18 at 07:07
  • Thankyou F0r3v3r-A-N00b that resolve my problem hahaha :D . i forget that one HAhahahaha i takes me hours to check every single line and only that As New is causing the problem thanks a lot :) and also thankyou jdweng for your comment :) thankyou (Y) – Adrian Adriano May 13 '18 at 07:27
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Tetsuya Yamamoto May 14 '18 at 02:31

2 Answers2

1

When you declare a variable like Dim cn As OleDb.OleDbConnection you are just telling the compiler what type it is not creating an object of that type.

When you use the New keyword OleDb.OleDbConnection is not just the name of a class (the data type) but it is an actual method. It is calling the constructor of the class which returns an instance of the object.

In C# you are required to put the parenthesis after like OleDb.OleDbConnection() which shows you are calling a method. You can add the parenthesis in vb.net but it is not required but I think it is a good reminder of the difference between setting a data type and creating an object.

Andre
  • 26,751
  • 7
  • 36
  • 80
Mary
  • 14,926
  • 3
  • 18
  • 27
0

Your declaration should be : Dim cn As New OleDb.OleDbConnection Dim cmd As New OleDb.OleDbCommand – F0r3v3r-A-N00b 20 mins ago