本文介绍了Nullreferenceexception未处理:对象引用未设置为对象的实例。为什么我收到这个错误?请帮帮我。我已经对此感到困惑了。拜托,拜托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我目前正在使用Visual Studio 2012 Pro和phpMyAdmin

,我对此问题感到困惑。我检查了很多次,但我不能

找到问题。有人请帮帮我?我需要向我的老师提交我的应用程序

。请帮帮我。



以下是代码:

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:

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.

推荐答案


这篇关于Nullreferenceexception未处理:对象引用未设置为对象的实例。为什么我收到这个错误?请帮帮我。我已经对此感到困惑了。拜托,拜托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 15:33