before insert sqlDataAdapter

S

Serdge Kooleman

i have a query: select id, name from Dictionary

field "id" is guid (so not null, and not identity)

i show only "name" in the dataGrid

how to set new id when i insert a new record?


currently VS wizard created insert command for me:

INSERT INTO Dictionary (id, name) VALUES (@id, @name );
SELECT id, name FROM Dictionary WHERE (id = @id)

it is not working "Column "id" doesn't allow nulls. Do you want to correct
the value?"

thank you
 
K

Kai Brinkmann [Microsoft]

Automatically generated updated/insert queries are based on the parameters
of the SELECT query. Since you are selecting both ID and name, the same
parameters end up in the generated queries. However, if ID is the primary
key (and you configured it to auto-increment in your DB), you don't need to
provide it as part of the insert query. A new ID will be generated
automatically when you insert a new record.
--
Kai Brinkmann [Microsoft]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Marina

Set the 'id' column in your DataTable to a value. Like Guid.NewGuid().

Or give the column a default of 'newid()' in the database
 
S

Serdge Kooleman

i mentioned that my id is not auto-increment. who use it?!
i have guid-based id.
how to update id in my case?
 
S

Serdge Kooleman

Set the 'id' column in your DataTable to a value. Like Guid.NewGuid().
how to update it? (i have DataSet)
do we have an event like "before insert" ?

Or give the column a default of 'newid()' in the database
i tried to do it. the same result.
 
K

Kai Brinkmann [Microsoft]

If the ID is a required value, you will have to figure out a way to create a
new ID before executing the INSERT query. Look at Marina's suggestion.
--
Kai Brinkmann [Microsoft]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Serdge Kooleman

Kai,

seems to me that u do not understand what i need.

i do not want to use newid() function in sql.
i want to generate new guid in C# ( System.Guid.NewGuid() )
could you give a code example? what event should i use?

where i should write: parameter['ID'] = 'value' ?
where ?
what event or something like this?

thank you



If the ID is a required value, you will have to figure out a way to create
a new ID before executing the INSERT query. Look at Marina's suggestion.

i know how to generate new ID.
in what place shoud i add this value to parameter?

thank you
 
M

Marina

At some point you are adding a row to this datatable. So where ever you add
the row, that's where you set the column value.
 
S

Serdge Kooleman

At some point you are adding a row to this datatable. So where ever you
add the row, that's where you set the column value.

Marina, i add row in DataGrid (winforms).
And i exactly need an advise: where to put code
parameter[id] = "something"

where? i do not see any places for that.... no any "before update event"
 
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 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.

I decided to use 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