how to insert value programmaticaly (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# or VB?
from the sql? (if i do "insert newid() ) it still give me an error message
:-(

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
 
R

Rykie

Ok the easiest is to make the id field a seed column of one, which
basically means that it will start at 0 and on every insert it will
automatically add 1 to it. I assume you are using MS SQL here. The
easiest is to go into enterprise manager and then go to design table
(right click on table and select) then click the column (id) then at
the bottom left are the properties, just set seed to true and the rest
will automatically happen, and save it.

Good luck
 
R

Rykie

one more, then you would only need to create a stored procedure to
accept the value of name, because id is automatically assigned.

eg: INSERT INTO Dictionary (name) VALUES(@name)
 
S

Serdge Kooleman

1. i'm not looking for the easiest way, sorry.
2. i use WinForms, not Web!
3. id have "uniqueidentifer" type. so i cannot use autoincrement for it ;-)
4. i want to update this field from VB (C#) without using additional SQL
statements like newid()

so i just want to repeat stuff that i did in Delphi a lot of times.
where i can put procedure like this:

onbeforeupdate
parameter['id'] = "{6F9619FF-8B86-D011-B42D-00C04FC92455}"
endproc

again, i'm using DataGrid with DataSet and sqlDataAdapter

tnank you
 
S

Serdge Kooleman

Ok. Seems to me that i found the right answer. Answer is on C#, but hope it
is easy to convert it to VB.

I decided to use the event dsDictionary1.Tables[0].ColumnChanging


in form constructor:

InitializeComponent();

dsDictionary1.Tables[0].ColumnChanging += new
DataColumnChangeEventHandler(OnColumnChanging);


protected static void OnColumnChanging(object sender,
DataColumnChangeEventArgs args)

{

if (args.Column.ColumnName.ToString() == "DictionaryName")

if (args.Row["id"] == System.DBNull.Value)

{

args.Row["id"] = System.Guid.NewGuid();

}

}


thank you to everybody who participate in the discussion!
appreciate your help
serge
 

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