Problem to find a record.
Hi, i want to do this: find a record and then if it exists show the information in various textboxes and a datagridview, i'm just trying to understand how things works because i'm new in Visual C#, so here it is the code that i'm using:
to show the record information:
| |
privatevoid VisualizarCD() { myDataSet =new DataSet("MusicaTeca"); daCD =new SqlDataAdapter("SELECT * FROM tbl_cds", myConnection); daCD.TableMappings.Add("Table", "tbl_scds"); daCD.Fill(myDataSet, "tbl_cds"); daTemas =new SqlDataAdapter("SELECT * FROM tbl_temas", myConnection); daTemas.TableMappings.Add("Table", "tbl_temas"); daTemas.Fill(myDataSet, "tbl_temas"); DataRelation relCD_Tema; DataColumn parentCol, childCol; parentCol = myDataSet.Tables["tbl_cds"].Columns["cd_id"]; childCol = myDataSet.Tables["tbl_temas"].Columns["cd_id"]; relCD_Tema =new DataRelation("RelCD_Temas", parentCol, childCol); myDataSet.Relations.Add(relCD_Tema); myDVM = myDataSet.DefaultViewManager; myDVM.DataViewSettings["tbl_cds"].Sort = "cd_id ASC"; cm = (CurrencyManager)this.BindingContext[myDVM, "tbl_cds"]; txtAlbum.DataBindings.Add("Text", myDVM, "tbl_cds.album"); txtAno.DataBindings.Add("Text", myDVM, "tbl_cds.ano"); txtArtista.DataBindings.Add("Text", myDVM, "tbl_cds.artista"); txtEditora.DataBindings.Add("Text", myDVM, "tbl_cds.editora"); txtGenero.DataBindings.Add("Text", myDVM, "tbl_cds.genero"); txtID.DataBindings.Add("Text", myDVM, "tbl_cds.cd_id"); dbGrid.DataSource = myDVM; dbGrid.DataMember = "tbl_cds.RelCD_Temas"; dbGrid.Columns["reg_id"].Visible =false; dbGrid.Columns["cd_id"].Visible =false; }
|
and to search for a record:
| |
privatevoid btnLocalizar_Click(object sender, EventArgs e) { int intRow = 0; intRow = myDataSet.Tables["tbl_cds"].DefaultView.Find(txtID.Text); if (intRow > -1) { cm.Position = intRow; } else { MessageBox.Show("Registo n?o localizado!"); } }
|
it give me this error in this line "cm.Position = intRow":
System.ArgumentException was unhandled
Column 'cd_id' is constrained to be unique. Value '9463216602' is already present.
i don't known if this is the right way for do such thing ... can someone help me please!!!!
Forgive me my bad English.
Thanks,
UltraMAX
[3257 byte] By [
UltraMAX] at [2007-12-17]