Hi,

I am having difficulty updating an existing record in a database through an
untyped dataset. Can't seem to work out what i am doing wrong, and have had
no luck finding any good examples on the net that match what i want to do.
Below is my code segment, currently i am getting an error that states
"Column 'Code' contains non-unique values" and i don't understand why it's
giving it to me, because there is only one record with that code and its the
one i am trying to update.

Any help would be appreciated.

DataRow tblRow ;
DataColumn[] pki = new DataColumn[1] ;

try {
DtlsConnection.ConnectionString = ConnectionPath ;
DtlsConnection.Open() ;
DtlsCommand.CommandText = "Select * From tbl" ;
DtlsCommand.Connection = DtlsConnection ;
DtlsAdapter.SelectCommand = DtlsCommand ;
DtlsAdapter.Fill(DtlsDs, "tbl") ;
pki[0] = SecurityDtlsDs.Tables["tbl"].Columns["Code"] ;
pki[0].Unique = true ;
DtlsDs.Tables["tbl"].PrimaryKey = pki ;

tblRow = DtlsDs.Tables["tbl"].Rows.Find(pCode) ;
tblRow.BeginEdit() ;
tblRow["Code"] = txtCode.Text ;
tblRow["Name"] = txtName.Text ;
tblRow["Type"] = txtType.Text ;
tblRow.EndEdit() ;
DtlsAdapter.Update(DtlsDs, "tbl") ;
//DtlsAdapter.Fill(DtlsDs, "tbl") ;
}
catch (Exception e) {
MessageBox.Show(e.Message, "Finwin", MessageBoxButtons.OK,
MessageBoxIcon.Error) ;
}

//Closing the connection
DtlsConnection.Close() ;


Kind Regards
Darryn Ross

Re: Untyped Dataset and Datarows??? by Per

Per
Thu Jul 10 06:20:02 CDT 2003

"Darryn Ross" <support@datawave.com.au> wrote in message
news:uols1DpRDHA.3132@tk2msftngp13.phx.gbl...
> Hi,
>
> I am having difficulty updating an existing record in a database through
an
> untyped dataset. Can't seem to work out what i am doing wrong, and have
had
> no luck finding any good examples on the net that match what i want to do.
> Below is my code segment, currently i am getting an error that states
> "Column 'Code' contains non-unique values" and i don't understand why it's
> giving it to me, because there is only one record with that code and its
the
> one i am trying to update.
>
> Any help would be appreciated.

Ok i don't know if it helps, but atleast i'll try ;)

> DataRow tblRow ;
> DataColumn[] pki = new DataColumn[1] ;
>
> try {
> DtlsConnection.ConnectionString = ConnectionPath ;
> DtlsConnection.Open() ;
> DtlsCommand.CommandText = "Select * From tbl" ;
> DtlsCommand.Connection = DtlsConnection ;
> DtlsAdapter.SelectCommand = DtlsCommand ;
> DtlsAdapter.Fill(DtlsDs, "tbl") ;
> pki[0] = SecurityDtlsDs.Tables["tbl"].Columns["Code"] ;
> pki[0].Unique = true ;

You don't need to explicitly say your primary key is Unique ;) Not sure if
it messes up things, but you don't have to set it.
Does the Code column in your Database have a primary key index? Or atleast a
Unique value? The error looks like it's telling you this field is no good
for a key value as there are several equal values - make sure this is your
primary key for your database or atleast set as unique in the database.

> DtlsDs.Tables["tbl"].PrimaryKey = pki ;
>
> tblRow = DtlsDs.Tables["tbl"].Rows.Find(pCode) ;
> tblRow.BeginEdit() ;

You're not checking if it's null, but then i guess it would throw an error
about object being null instead.

> tblRow["Code"] = txtCode.Text ;
> tblRow["Name"] = txtName.Text ;
> tblRow["Type"] = txtType.Text ;
> tblRow.EndEdit() ;
> DtlsAdapter.Update(DtlsDs, "tbl") ;
> //DtlsAdapter.Fill(DtlsDs, "tbl") ;
> }
> catch (Exception e) {
> MessageBox.Show(e.Message, "Finwin", MessageBoxButtons.OK,
> MessageBoxIcon.Error) ;
> }
>
> //Closing the connection
> DtlsConnection.Close() ;
>
>
> Kind Regards
> Darryn Ross
>
>