List Info

Thread: C-Sharp (C#) Group: Trying to get Primary Key using RowUpdated but get System.DBNull instead




C-Sharp (C#) Group: Trying to get Primary Key using RowUpdated but get System.DBNull instead
user name
2006-03-06 11:50:41
Hello.

I'm trying to get the primary key for a newly inserted row
using
ADO.NET and SqlServer.  The convention seems to be to do
this with the
RowUpdated event, i.e.

_sqlConnectionToTickerTextDb.Open();
SqlCommand cmdGetPK = new SqlCommand("SELECT IDENTITY",
_sqlConnectionToTickerTextDb);
object pkObject = cmdGetPK.ExecuteScalar();
int pkNunber = Int32.Parse((pkObject).ToString());
e.Row[_tickerIDColumnName] = pkNunber;
e.Row.AcceptChanges();
_sqlConnectionToTickerTextDb.Close();


However, I am getting the value System.DBNull instead of the
primary
key.
I''ve tried replacing IDENTITY with
SCOPE_IDENTITY() but the same
happens.
Is there a setting that I need to alter in the database or
am I
approaching this in the wrong way?

I've also noticed that most people dont use .Open and
.Close() in
coding the above, yet my app complains if I don't.

Is there an easier more practical way of getting the new
primary key
e.g. by looking at the attributes of the
SqlRowUpdatedEventArgs
parameter?

Greg.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Trying to get Primary Key using RowUpdated but get System.DBNull instead
user name
2006-03-07 06:24:03
Hi,
Why you're going in this way?
I think you must be using a stored procedure for the
insertion of the
of the row.
So in the very last line write a SELECT query to return your
current
identity value.
You can receive that into a dataset.
Hope this will solve your problem.
Regards,
Arindam Sinha


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Trying to get Primary Key using RowUpdated but get System.DBNull instead
user name
2006-03-07 09:26:31
Thanks Arindam.

I'm not using a stored procedure becuase at some point the
database may
be removed and I will just be using xml.

I've actually solved the problem by writing an insert
statement - this
gets the whole of the newly inserted row and updates my
datagrid. I did
hope to get it to work by looking at the rowupdated event,
but it was
taking too long to figure out!

Thanks again.

Greg.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )