Solved

Check If Record Is New Data

Posted on 2004-10-14
9
222 Views
Last Modified: 2011-09-20
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

Thanks
0
Comment
Question by:daverichardson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12310960
If DS.Tables("MyTable").Rows.Contains(me.textbox1.text) Then
    'Dont save
Else
    'Save
End If
0
 

Author Comment

by:daverichardson
ID: 12310995
i get an error
Input string was not in a correct format
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12311044
CHek the datatype. If you are looking for number do the cast.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:daverichardson
ID: 12311092
there are various columns in the table with various datatypes, but its only one column i need to search

Dave
0
 

Author Comment

by:daverichardson
ID: 12311098
can i specify the comumn?
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12311264
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
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12311307
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
0
 

Author Comment

by:daverichardson
ID: 12311321
could you point me in the right dirrection on how to do this
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 125 total points
ID: 12311469
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_DataSet.Tables.Add(m_dtPeople)
        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)
        m_dtPeople.Constraints.Add(unique_constraint)

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

        people_data(0) = "Bouf"
        people_data(1) = "Brian"
        people_data(2) = "Brian@nowhere.com"
        m_dtPeople.Rows.Add(people_data)

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

        ' 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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question