Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I'm currently using Visual Studio 2012 Pro with phpMyAdmin
and I'm confused with this problem. I checked it many times but I just couldn't
find the problem. Someone please help me? And I need to present my app
to my teacher. Please help me.

Here are the codes:
VB
Imports MySql.Data.MySqlClient
Public Class student_add
    'Create connection
    Dim MyConnection As Common.DbConnection

    'create data adapter
    Dim da As New MySqlDataAdapter
    'create dataset
    Dim ds As New DataSet()

    'declare connection string and query
    Dim MyConnString As String
    Dim sqlQRY As String

    Dim row As Integer

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim dt As DataTable = ds.Tables("student")
        Dim nRow As DataRow

        Try
            ' add a rowS
            nRow = dt.NewRow() '<<<--this is the error line--says: Object reference not set to an instance of an object.
            nRow("studID") = TextBox1.Text
            nRow("studLast") = TextBox2.Text
            nRow("studFirst") = TextBox3.Text
            nRow("studMiddle") = TextBox4.Text
            nRow("studCourse") = TextBox5.Text
            nRow("studYear") = TextBox6.Text
            nRow("studAddress") = TextBox7.Text
            nRow("studCellNum") = TextBox8.Text
            nRow("studGender") = ComboBox1.Text
            nRow("studStatus") = ComboBox2.Text

            dt.Rows.Add(nRow)
            'save in the room table
            da.Update(ds, "student")

            MsgBox("The record was successfully saved.", MsgBoxStyle.Information, "student INFORMATION")

            'Blank out the text boxes for new input/s
            TextBox1.Clear()
            TextBox2.Clear()
            TextBox3.Clear()
            TextBox4.Clear()
            TextBox5.Clear()
            TextBox6.Clear()
            TextBox7.Clear()
            TextBox8.Clear()
            ComboBox1.Text = ""
            ComboBox2.Text = ""

            'set the cursor on the first textbox
            TextBox1.Focus()

        Catch ex As MySqlException
            MsgBox(ex.ToString)
        End Try
    End Sub

Private Sub student_add_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'StudentDataSet.student' table. You can move, or remove it, as needed.
        Me.StudentTableAdapter1.Fill(Me.StudentDataSet.student)
        MyConnString = "datasource=localhost;empID=root;password=;database=studentsinfo"

        sqlQRY = "Select * from student"

        'create a connection to your database

        MyConnection = New MySqlConnection(MyConnString)

        Try

            ' Open connection

            MyConnection.Open()

            da.SelectCommand = New MySqlCommand(sqlQRY, MyConnection)

            'create command builder

            Dim cb As MySqlCommandBuilder = New MySqlCommandBuilder(da)

            'populate a DataTable (ds) with data from the database

            da.Fill(ds, "student")

            'show employee records in datagrid control

            dgvAdd.DataSource = ds

            dgvAdd.DataMember = "student"

        Catch ex As Common.DbException

            MsgBox(ex.ToString)

        Finally

            MyConnection.Close()

        End Try
    End Sub

    Private Sub dgvAdd_MouseUp(sender As Object, e As MouseEventArgs) Handles dgvAdd.MouseUp
        'when a row on the datagridview is clicked, the data is transferred to the textboxes;
        'first cell in the datagridview row starts at 0, second cell is 1, and so on
        TextBox1.Text = dgvAdd.CurrentRow.Cells(0).Value.ToString
        TextBox2.Text = dgvAdd.CurrentRow.Cells(1).Value.ToString
        TextBox3.Text = dgvAdd.CurrentRow.Cells(2).Value.ToString
        TextBox4.Text = dgvAdd.CurrentRow.Cells(3).Value.ToString
        TextBox5.Text = dgvAdd.CurrentRow.Cells(4).Value.ToString
        TextBox6.Text = dgvAdd.CurrentRow.Cells(5).Value.ToString
        TextBox7.Text = dgvAdd.CurrentRow.Cells(6).Value.ToString
        TextBox8.Text = dgvAdd.CurrentRow.Cells(7).Value.ToString
        ComboBox1.Text = dgvAdd.CurrentRow.Cells(8).Value.ToString
        ComboBox2.Text = dgvAdd.CurrentRow.Cells(9).Value.ToString

        'the row number/index of the tuple is taken note of (to be used when updating or deleting that tuple)
        row = dgvAdd.CurrentRow.Index
    End Sub
End Class


'Help me!!!

What I have tried:

I've tried changing the codes but nothing happened. And I also tried searching it in Google but I couldn't find the solution. Please help me with this.
Posted
Updated 1-Mar-16 2:33am
v2
Comments
F-ES Sitecore 1-Mar-16 8:06am    
"dt" will be null (Nothing) because ds.Tables("student") returned nothing probably because your code runs before the table is added to ds.

You really need to learn to debug your code and step through it so you can understand the order things are happening in, what is going on with your variables etc.

https://msdn.microsoft.com/en-us/library/sxw2ez55.aspx
CHill60 1-Mar-16 8:09am    
Where are you calling the code that populates ds?
[no name] 1-Mar-16 8:11am    
Put a break-point starting of Sub and start to debug it. See at which line it throws exception.

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900