before insert sqlDataAdapter

S

Serdge Kooleman

i have a query: select id, name from Dictionary
field "id" is uniqueidentifier (primary key id is not null, and not identity
!)
i show only "name" in the dataGrid

how to set up new id when i insert a new record? from the c#? from the sql?

INSERT INTO Dictionary (id, name) VALUES (@id, @name );

Always i'm getting message: "Column "id" doesn't allow nulls. Do you want to
correct
the value?"

thank you
 
B

Bart Mermuys

Hi,

Serdge Kooleman said:
i have a query: select id, name from Dictionary
field "id" is uniqueidentifier (primary key id is not null, and not
identity !)
i show only "name" in the dataGrid

how to set up new id when i insert a new record? from the c#? from the
sql?

INSERT INTO Dictionary (id, name) VALUES (@id, @name );

Always i'm getting message: "Column "id" doesn't allow nulls. Do you want
to correct
the value?"

This looks like an error message the DataGrid may show when you try to add a
new record. The error comes from the constraints on the DataTable and not
the insert command.

The DataTable (Net1.1) doesn't have a before-new-row-added event but because
it is bound to a DataGrid there is always a DataView in between and you can
use the DataView's ListChanged event to catch a new row (before it is
added):

//
// Put this after binding or
// in Form Load event (if you used designer to bind)
//
CurrencyManager cm = (CurrencyManager)
BindingContext[dataGrid1.DataSouce,dataGrid1.DataMember];

DataView dv = (DataView)cm.List;

dv.ListChanged+=
new ListChangedEventHandler(dv_ListChanged);


//
// then add the following event handler code
//
private void dv_ListChanged(object sender,
System.ComponentModel.ListChangedEventArgs e)
{
DataView dv = (DataView)sender;
if ( e.ListChangedType == ListChangedType.ItemAdded &&
dv[e.NewIndex].Row.RowState == DataRowState.Detached )
{
dv[e.NewIndex]["id"] = Guid.NewGuid();
}
}


Note that in NET 2.0 there will be easier ways of doing this.

HTH,
Greetings
 

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