Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do I avoid duplicated records error in Datagrid?

Posted on 2005-03-14
22
Medium Priority
?
292 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
  • 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
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: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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Loops Section Overview
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

571 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