Hello, I have a program connecting with a database..
so when add record in it, i want to validate for the duplication of ID in database..

here's my code for add button:

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        dSet.Tables(0).PrimaryKey = New DataColumn() {dSet.Tables(0).Columns("WatchID")} 'The table primary key is the WatchId
        Dim row As DataRow
        row = dSet.Tables(0).Rows.Find(txtWatchId.Text) 'the textbox id will be searched 
        If txtWatchId.Text = row("WatchID") Then 'validating duplicating id
            MsgBox("Watch ID already exist") 'messagebox to notify the user
            'here validating if the textboxes is empty
            'and a message will be displayed to notify the user
        ElseIf txtWatchId.Text = "" Then
            MsgBox("Watch ID is empty")
        ElseIf txtWatchBrand.Text = "" Then
            MsgBox("Watch Brand is empty")
        ElseIf txtWatchModel.Text = "" Then
            MsgBox("watch model is empty")
        ElseIf txtManufacturer.Text = "" Then
            MsgBox("Manufacturer is empty")
        ElseIf txtCountryOfOrigin.Text = "" Then
            MsgBox("Country of origin is empty")
        ElseIf txtDescription.Text = "" Then
            MsgBox("Description is empty")
        ElseIf cmbType.Text = "" Then
            MsgBox("Please choose a type")
        ElseIf txtPrice.Text = "" Then
            MsgBox("Price is empty")
        ElseIf Not IsNumeric(txtPrice.Text) Then 'validating if the price is not numeric
            MsgBox("Price is not numeric") 'message will be displayed
        ElseIf txtPriceInclVat.Text = "" Then
            MsgBox("Please calculate VAT")
        ElseIf Not IsNumeric(txtPriceInclVat.Text) Then 'validating if Price incl vat is not numeric
            MsgBox("Price plus VAT is not numeric") ' a message box is displayed to inform the user
        Else
            'After passing througth all these validation, the record is addes in the database
            Dim com As New OleDbCommand
            com.Connection = con
            'inserting the record in the watches table
            com.CommandText = "insert into Watches values('" & txtWatchId.Text & "','" & txtWatchBrand.Text & "','" & _
                                txtWatchModel.Text & "','" & txtManufacturer.Text & "','" & txtCountryOfOrigin.Text & "','" & _
                                cmbType.Text & "','" & txtDescription.Text & "'," & txtPrice.Text & "," & txtPriceInclVat.Text & ")"

            com.ExecuteNonQuery()
            MsgBox("Record Inserted") 'message box is displayed to inform the user that the record has been inserted in the database
            cmdUpdate.Enabled = True 'update button enabled
            cmdDelete.Enabled = True 'delete button enabled
            cmdAdd.Enabled = False 'add button enabled
            cmdfirst.Enabled = True 'first button enabled
            cmdPrevious.Enabled = True 'previous button enabled
            cmbNext.Enabled = True 'next button enabled
            cmdLast.Enabled = True 'last button enabled
            Call populate() 'populate procedure is called
            Call populate_fill() 'populate_fill is called
            Call fill_combo() 'fill_combo is called
        End If
    End Sub

here's the code for validation:

dSet.Tables(0).PrimaryKey = New DataColumn() {dSet.Tables(0).Columns("WatchID")} 'The table primary key is the WatchId
        Dim row As DataRow
        row = dSet.Tables(0).Rows.Find(txtWatchId.Text) 'the textbox id will be searched 
        If txtWatchId.Text = row("WatchID") Then 'validating duplicating id
            MsgBox("Watch ID already exist") 'messagebox to notify the user

so when i click on add, it says..NullReferenceException was unhandled

see screenshot below

thanks

Recommended Answers

All 3 Replies

Member Avatar for Unhnd_Exception

The DataRowCollection returns null if nothing is found.

If its nothing then it wasn't found.

Dim row As DataRow
row = dSet.Tables(0).Rows.Find(txtWatchId.Text) 'the textbox id will be searched 
If row isnot nothing Then 'validating duplicating id
    MsgBox("Watch ID already exist") 'messagebox to notify the user

The DataRowCollection returns null if nothing is found.

If its nothing then it wasn't found.

Dim row As DataRow
row = dSet.Tables(0).Rows.Find(txtWatchId.Text) 'the textbox id will be searched 
If row isnot nothing Then 'validating duplicating id
    MsgBox("Watch ID already exist") 'messagebox to notify the user

thanks.. works great
thank you very much

you can simply set the ID column as a primary key in the DBMS

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.