ReadOnlyException on Primary Key Column

J

Jim Chapman

I'm getting a ReadOnlyException on the Primary Key Column of a DataTable in
my DataSet. I'm using the da.RowUpdated and SELECT @@IDENTITY trick to grab
the pkValue from SQL 7 and update the DataRow. However, this fails and the
DataRow keeps the value it auto-created (usually 0.)



I'm using OleDb and, oddly enough, the exact same code works with Access
without a problem.



Any suggestions?
 
M

Miha Markic

Hi Jim,

Your table pk column is marked as readonly.
Set readonly = false prior to updating it and reset it after.
Why don't you rather put SELECT @@IDENTITY after insert statament and let
Update method handle it?
 
J

Jim Chapman

Miha,

I'm using FillSchema to define the columns. For some reason FillSchema
allows the write to the DataRow primary key column with Access, but not SQL
Server. This is my first project with SQL Server and, in hindsight, it
would seem natural for FillSchema to make the DataRow column readonly. I set
the column to readonly = false, by hand, as a work around. (This sorta
defeats the convenience of FillSchema.) I'm curious as how to get FillSchema
to behave like it does with Access.

Also, I'm not sure what you mean by; "Why don't you rather put SELECT
@@IDENTITY after insert statement and let Update method handle it?"

Thanks
 
M

Miha Markic

Hi Jim,

Jim Chapman said:
Miha,

I'm using FillSchema to define the columns. For some reason FillSchema
allows the write to the DataRow primary key column with Access, but not SQL
Server. This is my first project with SQL Server and, in hindsight, it
would seem natural for FillSchema to make the DataRow column readonly. I set
the column to readonly = false, by hand, as a work around. (This sorta
defeats the convenience of FillSchema.) I'm curious as how to get FillSchema
to behave like it does with Access.

I would suggest to create strong typed dataset at design time.
Also, I'm not sure what you mean by; "Why don't you rather put SELECT
@@IDENTITY after insert statement and let Update method handle it?"

See
Updating the Database with a DataAdapter and the DataSet
..net help topic.
 
W

www.ttdown.com

try to change the column propertie to ReadOnly = false;

NO problems.

try{



dsPeticiones1.Tables["TratamientoPeticionesLog"].Columns["IdPeticion"].ReadOnly
= false;


DataRow RowPeticion =
dsPeticiones1.Tables["TratamientoPeticionesLog"].NewRow();
RowPeticion["NumPeticion"]
= "AAAAAA";
RowPeticion["Expediente"]
= "BBBBBB" ;
RowPeticion["ResultadoTratamiento"]
= 1;
RowPeticion["MensajeErrorTratamiento"]
= "Correcto";
RowPeticion["HoraTratamiento"]
= DateTime.Now;


dsPeticiones1.Tables["TratamientoPeticionesLog"].Rows.Add(RowPeticion);
odbcDataAdapter1.Update(dsPeticiones1,
"TratamientoPeticionesLog");

}
catch(System.Data.ReadOnlyException ex){

}


private void odbcDataAdapter1_RowUpdated(object sender,
System.Data.Odbc.OdbcRowUpdatedEventArgs e) {

if (e.Status == UpdateStatus.Continue &&
e.StatementType == StatementType.Insert ) {
// Obtener el valor de la columna
Identity
e.Row["IdPeticion"] =
Int32.Parse(cmdGetIdentity.ExecuteScalar().ToString());
e.Row.AcceptChanges();
}


}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top