ERROR RECEIVED: NullReferenceException was unhandled
Object reference not set to an instance of an object.

And it points on the particular line: dsNewRow = ds.Tables("Employees").NewRow()

Imports System.Data.OleDb
Public Class Register
    Public cn As New OleDbConnection
    Public rd As OleDbDataReader
    Public da As New OleDbDataAdapter
    Public ds As New DataSet
    Public str As String
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

        dsNewRow = ds.Tables("Employees").NewRow

        dsNewRow.Item("Given Name") = txtGivenName.Text
        dsNewRow.Item("Last Name") = txtLastName.Text
        dsNewRow.Item("Gender") = cmbGender.Text
        dsNewRow.Item("Month") = cmbMonth.Text
        dsNewRow.Item("Day") = cmbDay.Text
        dsNewRow.Item("Year") = cmbYear.Text
        dsNewRow.Item("Address") = txtAddress.Text
        dsNewRow.Item("Email Address") = txtEmailAddress.Text
        dsNewRow.Item("Username") = txtUsername.Text
        dsNewRow.Item("Password") = txtPassword.Text
        dsNewRow.Item("Secret Question 1") = cmbSecretQ1.Text
        dsNewRow.Item("Secret Question 2") = cmbSecretQ2.Text
        dsNewRow.Item("Secret Answer 1") = txtSecretA1.Text
        dsNewRow.Item("Secret Answer 2") = txtSecretA2.Text

        ds.Tables("Employees").Rows.Add(dsNewRow)
        da.Update(ds, "Employees")

        MsgBox("New employee added to the database!")
        SuccessRegister.ShowDialog()
        Profile.Show()
        Me.Hide()
    End Sub
End Class

Recommended Answers

All 12 Replies

ds refers to what ?

an unnamed dataset, just something to accommodate the data we keep on our MS Access Database (msn_pharmacy.mdb)

does you dataset refers to your database (i can't find that in your code) ?

I'm in doubt about it, so far, here's my other piece of code, but this time, it mentions on what Database we're trying to add, edit, delete, and read to.

Imports System.Data.OleDb

Public Class LoginSub
    Public cn As New OleDbConnection
    Public rd As OleDbDataReader
    Public da As New OleDbDataAdapter
    Public str As String

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()

    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Register.Show()
        Me.Hide()

    End Sub
    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        ForgotPassword.Show()
        Me.Hide()

    End Sub

    Private Sub Logon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Logon.Click
        If User.Text = "" And Pass.Text = "" Then
            MessageBox.Show("Please Input a Username and Password")
        ElseIf User.Text = "" Then
            MessageBox.Show("Please Input a Username")
        ElseIf Pass.Text = "" Then
            MessageBox.Show("Please Input a Password")
        Else
            str = "select * from Employees where Username='" & User.Text & _
            "' and Password='" & Pass.Text & "'"
            Dim cmd As New OleDb.OleDbCommand
            cmd.CommandText = str
            cmd.Connection = cn
            da.SelectCommand = cmd
            rd = cmd.ExecuteReader
            If (rd.Read() = True) Then
                MsgBox("WELCOME to MSN Pharmacy!")
                welcome.Show()
                Me.Hide()
                welcome.Timer1.Start()
                welcome.Progress.Increment(1)
            Else
                MsgBox("Incorrect input username/password")
            End If
        End If

    End Sub

    Private Sub LoginSub_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0; data source=C:\Documents and Settings\James\My Documents\MSN Pharmacy\msn_pharmacy.mdb"
        cn.Open()
    End Sub
End Class

Hi,

As Debasisdas subtly points out, where do you populate DS?

In your code you've given us so far DS is an unnamed, empty dataset so where is DS.Tables("Employees") coming from? If you haven't populated DS then the DS.Tables collection is empty and you can not add data to a non existant table.

If you have but just haven't shown us the code then how are you doing it? I see you starting to put a command builder together for a dataadaptor but you don't seam to do anything with it....

Go through the tutorial properly.

Imports System.Data.OleDb
Public Class Register
    Public cn As New OleDbConnection
    Public rd As OleDbDataReader
    Public str As String

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ds As New DataSet
        Dim da As New OleDbDataAdapter
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

        da = New OleDb.OleDbDataAdapter(str, cn)
        da.Fill(ds, "Employees")

        dsNewRow = ds.Tables("Employees").NewRow()

        dsNewRow.Item("Given Name") = txtGivenName.Text
        dsNewRow.Item("Last Name") = txtLastName.Text
        dsNewRow.Item("Gender") = cmbGender.Text
        dsNewRow.Item("Month") = cmbMonth.Text
        dsNewRow.Item("Day") = cmbDay.Text
        dsNewRow.Item("Year") = cmbYear.Text
        dsNewRow.Item("Address") = txtAddress.Text
        dsNewRow.Item("Email Address") = txtEmailAddress.Text
        dsNewRow.Item("Username") = txtUsername.Text
        dsNewRow.Item("Password") = txtPassword.Text
        dsNewRow.Item("Secret Question 1") = cmbSecretQ1.Text
        dsNewRow.Item("Secret Question 2") = cmbSecretQ2.Text
        dsNewRow.Item("Secret Answer 1") = txtSecretA1.Text
        dsNewRow.Item("Secret Answer 2") = txtSecretA2.Text

        ds.Tables("Employees").Rows.Add(dsNewRow)
        da.Update(ds, "Employees")

        MsgBox("New employee added to the database!")
        SuccessRegister.ShowDialog()
        Profile.Show()
        Me.Hide()
    End Sub

    Private Sub Register_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        cn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0; data source=C:\Documents and Settings\James\My Documents\MSN Pharmacy\msn_pharmacy.mdb"
        cn.Open()
    End Sub
End Class

Another attempt, this time introducing the DataSet on registration module.

I use vb 2008 express edition. deleting from the my database is a problem.i use the SQL Sever 2005 express to design my database

Hi
If your using SQL Server you should for security reasons never write your queries on the fly, use stored procedures and give the user only access to excute stored procedures and views.


The good news is you need only write a stored procedure to delete whatever record you pass in as parameters and then just execute it in your code.

Is it just me or is the str empty?

commented: lack of clarity -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.