Solved

Check If Record Is New Data

Posted on 2004-10-14
9
210 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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