Check If Record Is New Data

How do I check to see if new data entered into a bound textbox already exists in the table?
If it doesn’t then I need to fill some fields
However if it already exists I need to check if the text in another textbox matches a field an the current record, if it does then I need to fill some different fields

Who is Participating?
iboutchkineConnect With a Mentor Commented:
Here is an example how to create datatable with unique filelds

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private m_DataSet As DataSet
    Private m_dtPeople As DataTable

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Build the DataSet.
        m_DataSet = New DataSet()

        ' Build the People table.
        m_dtPeople = New DataTable("People")
        m_dtPeople.Columns.Add("LastName", GetType(String))
        m_dtPeople.Columns.Add("FirstName", GetType(String))
        m_dtPeople.Columns.Add("Notes", GetType(String))

        ' Make the UniqueConstraint on LastName
        ' and FirstName.
        Dim data_columns(1) As DataColumn
        Dim unique_constraint As UniqueConstraint

        data_columns(0) = m_dtPeople.Columns("LastName")
        data_columns(1) = m_dtPeople.Columns("FirstName")
        unique_constraint = New UniqueConstraint(data_columns)

        ' Populate the People table.
        Dim people_data(2) As String
        people_data(0) = "Abercrombe"
        people_data(1) = "Alice"
        people_data(2) = ""

        people_data(0) = "Bouf"
        people_data(1) = "Brian"
        people_data(2) = ""

        people_data(0) = "Carnivale"
        people_data(1) = "Cynthia"
        people_data(2) = "Phone: 987-654-3210"

        ' Attach the DataGrid to the DataTable.
        dgPeople.DataSource = m_dtPeople
    End Sub
End Class

Now if you try to add duplicate field you will get an exception. Put your save record to the try-catch block and catch this exception
If DS.Tables("MyTable").Rows.Contains(me.textbox1.text) Then
    'Dont save
End If
daverichardsonAuthor Commented:
i get an error
Input string was not in a correct format
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

CHek the datatype. If you are looking for number do the cast.
daverichardsonAuthor Commented:
there are various columns in the table with various datatypes, but its only one column i need to search

daverichardsonAuthor Commented:
can i specify the comumn?
This code works for the primary key column. I f you want to check if this value exist the col must be primary key.
If it is not a case you have to create datareader for thsi column and then and loop through it
Another approach would be to create unique field for this column and then put try catch block when you saving a record.
Let me know if you need help wiht unique field
daverichardsonAuthor Commented:
could you point me in the right dirrection on how to do this
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.