PC Review


Reply
Thread Tools Rate Thread

before insert sqlDataAdapter

 
 
Serdge Kooleman
Guest
Posts: n/a
 
      26th Sep 2005
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


 
Reply With Quote
 
 
 
 
Bart Mermuys
Guest
Posts: n/a
 
      27th Sep 2005
Hi,

"Serdge Kooleman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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




>
> thank you
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using SqlDataAdapter for Insert but not Update? Stu Microsoft C# .NET 7 16th Dec 2009 04:22 PM
how to insert value programmaticaly (sqlDataAdapter) Serdge Kooleman Microsoft VB .NET 5 28th Sep 2005 05:34 PM
before insert sqlDataAdapter Serdge Kooleman Microsoft C# .NET 11 28th Sep 2005 05:33 PM
how to set up field in sqlDataAdapter before insert? Serdge Kooleman Microsoft C# .NET 2 26th Sep 2005 11:52 PM
sqlDataAdapter Insert C++ =?Utf-8?B?bmJvaGFuYQ==?= Microsoft Dot NET Framework Forms 0 30th Dec 2004 01:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:58 AM.