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
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)