I don't want duplicate records

Hello to everybody!!!

I'm newbie programming in vb.net. I’m actually programming an aplication that use a small database in SQL Server Express. When I load a form that contains a DataGridView I show the contents of a table as follows:

Private Sub frmModificaDatosConsumidor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SqlDataAdapter1.SelectCommand.CommandText = "SELECT RFC, Nombre, Direccion, Colonia, CodPostal, Telefono, Ciudad FROM tblDatosConsumidor"

SqlConnection1.Open()

SqlDataAdapter1.Fill(DataSet1, "Consumidor")

SqlConnection1.Close()

DataGridView1.DataSource = DataSet1.Tables("Consumidor")

End Sub

If I close the form and load it again the records of the database they apppear duplicates. The only soluion I’ve found is delete the dataset

Private Sub frmModificaDatosConsumidor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 '---the code as I use actually:

SqlDataAdapter1.SelectCommand.CommandText = "SELECT RFC, Nombre, Direccion, Colonia, CodPostal, Telefono, Ciudad FROM tblDatosConsumidor"

SqlConnection1.Open()

DataSet1.Clear()  '<------Here I delete the dataset

SqlDataAdapter1.Fill(DataSet1, "Consumidor")

SqlConnection1.Close()

DataGridView1.DataSource = DataSet1.Tables("Consumidor")

End Sub

Is there any form to avoid this behavior

Thanks in advanced for any Help

P.D. sorry for mi English. I hope you understand me.

Jose Eloy

Mexico



Answer this question

I don't want duplicate records

  • olapdummy

    Thanks, shakalama, but this in not my problem. I think maybe there is an property in the design mode of the database to indicate must be an unique value for a record. Is this true

    Regards.

    Jose Eloy
    Mexico


  • Vladimir S.

    hi,

    yes there are away to make a particular columen in a table contain unique values when you design your dataset like this, i made a dataset called mydataset and a table called "_cat" and gave it premerykey field

    Dim mydataset As New DataSet

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'creating the catigory table

    Dim Tbcat As New DataTable("_cat")

    Tbcat.Columns.Add("catid", GetType(Integer))

    Tbcat.Columns.Add("catname", GetType(String))

    Tbcat.Columns("catid").Unique = True

    Tbcat.PrimaryKey = New DataColumn() {Tbcat.Columns("catid")}

    With Tbcat.Columns("catid")

    .AutoIncrement = True

    .AutoIncrementSeed = 1

    End With

    'binding the tables to the dataset

    mydataset.Tables.Add(Tbcat)

     



  • Boris

    hi,

    i think if you used data menu and add datasource and chosed a dataset to your database and then you can put a dataset to your form that will  be better, i'm new like you and that saves lots of time to me, and let me think about other things now at least.

    anyway you can put it manualy like what you did but i guess if you used conditional statment it will be better like

    If dataset1.tblDatosConsumidor.rows.count = 0 then

    you can add your connection here

    end if

     

    so if you loaded the form again and there are rows in the dataset it will not add other rows



  • I don't want duplicate records