?
Solved

How do I avoid duplicated records error in Datagrid?

Posted on 2005-03-14
22
Medium Priority
?
288 Views
Last Modified: 2010-04-23
Hi,
How do I avoid duplicated records ERROR in Datagrid when I have Num as a primary key and I am inputting like this:

Private Sub cmdAddNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddLoan.Click
        Dim i As Integer
        Dim rw As DataRow
        rw = DataSet11.Tables(0).NewRow
        rw.Item("Num") = txtLoanNumToAdd.Text
        Try
            DataSet11.Tables(0).Rows.Add(rw)
            i = OleDbDataAdapter1.Update(DataSet11)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
0
Comment
Question by:Mylor
[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
  • 11
  • 10
22 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 total points
ID: 13540980
assuming that "Num" is your primary key field

Private Sub cmdAddNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddLoan.Click
        Dim i As Integer
        Dim rw As DataRow
        If Not DataSet11.Tables(0).rows.find(txtLoanNumToAdd.Text) Is Nothing Then
           messagebox.show("Duplicate row")
           return
        end if
        rw = DataSet11.Tables(0).NewRow
        rw.Item("Num") = txtLoanNumToAdd.Text
        Try
            DataSet11.Tables(0).Rows.Add(rw)
            i = OleDbDataAdapter1.Update(DataSet11)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13542812
Adding a primary key in the DataTable will do the trick.

Cheers
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13542966
DataTable DT1;
...
...
DT1.PrimaryKey = DT1.Columns[0];

OR

DataColumn [] primarykeyscomb = new DataColumn[2];
primarykeyscomb[0] =  DT1.Columns[0];
primarykeyscomb[1] =  DT1.Columns[1];
DT1.PrimaryKey = primarykeyscomb;

Hope this helps...
Cheers
0
Technology Partners: 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:Mylor
ID: 13546514
Yes but it give a "Fatal exception error..." that I'm trying to avoid.
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13546570
Use should try and use the try-catch blocks and capture the exception...
0
 

Author Comment

by:Mylor
ID: 13546578
how?
0
 

Author Comment

by:Mylor
ID: 13546632
Also, BriCrowe, this code is giving me an error: "The table does not have a primary key", which it does.


        If Not DataSet11.Tables(0).Rows.Find(txtLoanNumToAdd.Text) Is Nothing Then
            MessageBox.Show("Duplicate row")
            Return
        End If
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13546686
Try
            DataSet11.Tables(0).Rows.Add(rw)
            i = OleDbDataAdapter1.Update(DataSet11)
        Catch ex As PrimaryKeyException (whatever)
           
                   MessageBox.Show("This is a duplicate record"))
        End Try
0
 

Author Comment

by:Mylor
ID: 13546708
rajaloysious,
I already have that in the code.
0
 

Author Comment

by:Mylor
ID: 13546721
Sorry, is different. What is (whatever)
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13546750
whatever is the exception which actuall happens on entering  a duplicate record.
if that doesn't help
Can you pass on your code so that i can have a look.
0
 

Author Comment

by:Mylor
ID: 13546770
it is at the very begining of this question.
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13547294
I wrote this and it works fine...
                  try
                  {
                        DataTable dt1 = new DataTable();
                        dt1.Columns.Add("sdfs", Type.GetType("System.Int64"));                        
                        dt1.Columns.Add("sdfs2", Type.GetType("System.Int64"));
                        
                        DataColumn [] keys = new DataColumn[1];
                        keys[0] =dt1.Columns[0];
                        dt1.PrimaryKey =keys ;

                        DataRow dt1row  = dt1.NewRow();
                        dt1row[0] = 1;
                        dt1row[1] = 2;
                        dt1.Rows.Add(dt1row);
                        DataRow dt1row2  = dt1.NewRow();
                        dt1row2[0] = 1;
                        dt1row2[1] = 2;
                        dt1.Rows.Add(dt1row2);
                  }

                  catch (System.Data.ConstraintException ex)
                  {
                        MessageBox.Show("Duplicate Values !!");
                  }
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13547312
Oops it is in c#...
Try
            DataSet11.Tables(0).Rows.Add(rw)
            i = OleDbDataAdapter1.Update(DataSet11)
        Catch ex As System.Data.ConstraintException            
                   MessageBox.Show("This is a duplicate record"))
        End Try
0
 

Author Comment

by:Mylor
ID: 13547323
this is C# and I'm workin in VB.NET
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13547351
Sorry.. sent the VB.NET a few moments ago
0
 

Author Comment

by:Mylor
ID: 13547358
Nope, it does not work.
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13547469
This works for me in VB.NET.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DT1 As New DataTable
        DT1.Columns.Add("SDF1", Type.GetType("System.Int64"))
        Dim dc(1) As DataColumn
        dc(0) = DT1.Columns(0)
        DT1.PrimaryKey = dc

        Dim i As Integer
        Dim rw As DataRow
        Dim rw2 As DataRow
        Try
            rw = DT1.NewRow
            rw(0) = 1
            DT1.Rows.Add(rw)
            rw2 = DT1.NewRow
            rw2(0) = 1
            DT1.Rows.Add(rw2)

        Catch ex As System.Data.ConstraintException
            MessageBox.Show("Duplicate Value")
        End Try
    End Sub
0
 

Author Comment

by:Mylor
ID: 13547874
I change something in BriCrowe  solution and it worked. Thanks guys !

Thank you rajaloysious !
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13556771
Great news !! that you have solved it.

But i would like to know what you added to make that work, because without adding a primary key it would never work.
Also i executed the code which i sent you last and it worked perfectly fine..

cheers
0
 

Author Comment

by:Mylor
ID: 13557791
The primary key was already added in the database but I had to fix the code . Thanks.
0
 
LVL 8

Expert Comment

by:rajaloysious
ID: 13557906
I meant to say Rows.Find will not work in your code if you dont have a primary key set to the datatable
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
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 is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 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