Solved

Check If Record Is New Data

Posted on 2004-10-14
9
217 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL LINE CONTINUATION ISSUE 12 37
write xml in vb.net 2 32
run a stored procedure from vb.net 1 27
ASP.NET/VB: Convert Date and Time to YYYY-MM-DDTHH:MM:SS 3 10
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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