This has been addressed several times and I've read several articles and
posts and I still can't get this to work. (This is VB .NET)

My understanding is that if I append a select statement to my DataAdapter's
Insert statement that I should be able to get an updated resultset. I've
got this:

INSERT INTO dbo.FD_Files (FileName, ExpireTime, PostedBy, HashName,
FileSize, Description) VALUES (@FileName, @ExpireTime, @PostedBy, @HashName,
@FileSize, @Description); SELECT FileID, FileName, ExpireTime, PostedBy,
HashName, FileSize, Description FROM dbo.FD_Files WHERE FileID = @@IDENTITY

I've also tried using SCOPE_IDENTITY() - reagrdless, I always get the whole
table returned.

I need to know the identity of the record I just added and I keep getting a
whole table. It's making me crazy.

I've even tried setting the adapter's select command to SELECT FileID from
FD_Files WHERE FileID = SCOPE_IDENTITY() and re-filling the dataset. I get
the whole damned table. Why!!!!????

What am I missing? I've tried using the built in wizard which comes up with
an insert statement just like the one above and I still end up with the
whole table after I update.

Can anyone shed some light on this? Thanks.

Jerry

Re: I Have No Identity by Miha

Miha
Fri Feb 20 04:07:46 CST 2004

Hi Jerry,

Scope_identity() is the right one.
About your problem: what do you mean that you get the whole table?
What is your scenario?
--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Jerry Camel" <rlrcstr@msn.com> wrote in message
news:%23taobN09DHA.1816@TK2MSFTNGP12.phx.gbl...
> This has been addressed several times and I've read several articles and
> posts and I still can't get this to work. (This is VB .NET)
>
> My understanding is that if I append a select statement to my
DataAdapter's
> Insert statement that I should be able to get an updated resultset. I've
> got this:
>
> INSERT INTO dbo.FD_Files (FileName, ExpireTime, PostedBy, HashName,
> FileSize, Description) VALUES (@FileName, @ExpireTime, @PostedBy,
@HashName,
> @FileSize, @Description); SELECT FileID, FileName, ExpireTime, PostedBy,
> HashName, FileSize, Description FROM dbo.FD_Files WHERE FileID =
@@IDENTITY
>
> I've also tried using SCOPE_IDENTITY() - reagrdless, I always get the
whole
> table returned.
>
> I need to know the identity of the record I just added and I keep getting
a
> whole table. It's making me crazy.
>
> I've even tried setting the adapter's select command to SELECT FileID from
> FD_Files WHERE FileID = SCOPE_IDENTITY() and re-filling the dataset. I
get
> the whole damned table. Why!!!!????
>
> What am I missing? I've tried using the built in wizard which comes up
with
> an insert statement just like the one above and I still end up with the
> whole table after I update.
>
> Can anyone shed some light on this? Thanks.
>
> Jerry
>
>



Re: I Have No Identity by Jerry

Jerry
Fri Feb 20 08:29:02 CST 2004

This is a multi-part message in MIME format.

------=_NextPart_000_000C_01C3F793.FA6370E0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I've got everythign stripped down to just the basics - taking my lead =
from a kb article. All I'm doing at this point is adding a single =
record to a table and trying to get the identity (FileID) value back so =
I can add recs to a related table. Here's my code:

----------------------------------------------------------------------
Dim sqlCnxn As New SqlConnection("user =
id=3DSomeUser;password=3Dpassword;data source=3DMYSERVER")
Dim cmdFiles As New SqlCommand()

Dim sFileName As String
sFileName =3D Now.Millisecond & Now.Second & Now.Minute & Now.Hour & =
Now.Year & Now.Month & Now.Day

Dim daFiles As New SqlDataAdapter("SELECT FileID, FileName, ExpireTime, =
PostedBy, HashName, FileSize, Description FROM FD_Files", sqlCnxn)

Dim dsFiles As New DataSet()
Dim sqlInsert As New SqlCommand()

With sqlInsert
.CommandText =3D "INSERT INTO FD_Files (FileName, ExpireTime, =
PostedBy, HashName, FileSize, Description) " & _
"VALUES (@FileName, @ExpireTime, @PostedBy, @HashName, =
@FileSize, @Description); " & _
"SELECT FileID, FileName, ExpireTime, PostedBy, =
HashName, FileSize, Description " & _
"FROM FD_Files WHERE (FileID =3D Scope_Identity())"
.CommandType =3D CommandType.Text
.Connection =3D sqlCnxn
.Parameters.Add(New SqlParameter("@FileName", SqlDbType.VarChar, 300, =
"FileName"))
.Parameters.Add(New SqlParameter("@ExpireTime", SqlDbType.DateTime, =
4, "ExpireTime"))
.Parameters.Add(New SqlParameter("@PostedBy", SqlDbType.Char, 25, =
"PostedBy"))
.Parameters.Add(New SqlParameter("@HashName", SqlDbType.VarChar, 25, =
"HashName"))
.Parameters.Add(New SqlParameter("@FileSize", SqlDbType.VarChar, 10, =
"FileSize"))
.Parameters.Add(New SqlParameter("@Description", SqlDbType.VarChar, =
512, "Description"))
End With

daFiles.InsertCommand =3D sqlInsert
daFiles.Fill(dsFiles, "FD_Files")

Dim dr As DataRow =3D dsFiles.Tables(0).NewRow
Dim dFileSize As Double =3D 1234567 / 1024 / 1024
If dFileSize < 0.01 Then dFileSize =3D 0.01

dr("FileName") =3D "TestFileName"
dr("ExpireTime") =3D Now.AddHours(24)
dr("PostedBy") =3D "JerryCa"
dr("HashName") =3D sFileName
dr("FileSize") =3D Format(dFileSize, "N") & " MB"
dr("Description") =3D "Test Description"

dsFiles.Tables(0).Rows.Add(dr)
daFiles.Update(dsFiles, "FD_Files")
dsFiles.AcceptChanges()

Dim i As Int16
For i =3D 0 To dsFiles.Tables("FD_Files").Rows.Count - 1
With dsFiles.Tables("FD_Files")
Debug.WriteLine("FileID: " & .Rows(i)(0).ToString)
Debug.WriteLine("FileName: " & .Rows(i)(1).ToString)
Debug.WriteLine("ExpireTime: " & .Rows(i)(2).ToString)
End With
Next i
----------------------------------------------------------------------

Now, I assumed that the SELECT attached to the INSERT would cause the =
updated dataset to contain only the new record because of the WHERE =
clause. But the updated ds always has the full table. How am I =
supposed to know which is the new record? Is it safe to assume that the =
last record in the ds is the new one? According th the KB artice I got =
this code from, I should see "The new record", not all records. What am =
I missing? Thanks!

Jerry


"Miha Markic [MVP C#]" <miha at rthand com> wrote in message =
news:O0Cwhl59DHA.1128@tk2msftngp13.phx.gbl...
> Hi Jerry,
>=20
> Scope_identity() is the right one.
> About your problem: what do you mean that you get the whole table?
> What is your scenario?
> --=20
> Miha Markic [MVP C#] - RightHand .NET consulting & software =
development
> miha at rthand com
> www.rthand.com
>=20
> "Jerry Camel" <rlrcstr@msn.com> wrote in message
> news:%23taobN09DHA.1816@TK2MSFTNGP12.phx.gbl...
> > This has been addressed several times and I've read several articles =
and
> > posts and I still can't get this to work. (This is VB .NET)
> >
> > My understanding is that if I append a select statement to my
> DataAdapter's
> > Insert statement that I should be able to get an updated resultset. =
I've
> > got this:
> >
> > INSERT INTO dbo.FD_Files (FileName, ExpireTime, PostedBy, HashName,
> > FileSize, Description) VALUES (@FileName, @ExpireTime, @PostedBy,
> @HashName,
> > @FileSize, @Description); SELECT FileID, FileName, ExpireTime, =
PostedBy,
> > HashName, FileSize, Description FROM dbo.FD_Files WHERE FileID =3D
> @@IDENTITY
> >
> > I've also tried using SCOPE_IDENTITY() - reagrdless, I always get =
the
> whole
> > table returned.
> >
> > I need to know the identity of the record I just added and I keep =
getting
> a
> > whole table. It's making me crazy.
> >
> > I've even tried setting the adapter's select command to SELECT =
FileID from
> > FD_Files WHERE FileID =3D SCOPE_IDENTITY() and re-filling the =
dataset. I
> get
> > the whole damned table. Why!!!!????
> >
> > What am I missing? I've tried using the built in wizard which comes =
up
> with
> > an insert statement just like the one above and I still end up with =
the
> > whole table after I update.
> >
> > Can anyone shed some light on this? Thanks.
> >
> > Jerry
> >
> >
>=20
>
------=_NextPart_000_000C_01C3F793.FA6370E0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1400" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff background=3D"">
<DIV><FONT face=3DArial size=3D2>I've got everythign stripped down to =
just the=20
basics - taking my lead from a kb article.&nbsp; All I'm doing at this =
point is=20
adding a single record to a table and trying to get the identity =
(FileID) value=20
back so I can add recs to a related table.&nbsp; Here's my =
code:<BR><BR><FONT=20
face=3D"Courier =
New">--------------------------------------------------------------------=
--</FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Courier New">Dim sqlCnxn =
As New=20
SqlConnection("user id=3DSomeUser;password=3Dpassword;data =
source=3DMYSERVER")<BR>Dim=20
cmdFiles As New SqlCommand()<BR></DIV></FONT></FONT>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Courier New">Dim =
sFileName As=20
String<BR>sFileName =3D Now.Millisecond &amp; Now.Second &amp; =
Now.Minute &amp;=20
Now.Hour &amp; Now.Year &amp; Now.Month &amp; =
Now.Day<BR></FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Courier New">Dim daFiles =
As New=20
SqlDataAdapter("SELECT FileID, FileName, ExpireTime, PostedBy, HashName, =

FileSize, Description FROM FD_Files", sqlCnxn)<BR></FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Courier New">Dim dsFiles =
As New=20
DataSet()<BR>Dim sqlInsert As New SqlCommand()<BR></FONT></FONT></DIV>
<DIV><FONT face=3DArial size=3D2><FONT face=3D"Courier New">With=20
sqlInsert<BR>&nbsp;&nbsp; .CommandText =3D "INSERT INTO FD_Files =
(FileName,=20
ExpireTime, PostedBy, HashName, FileSize, Description) " &amp;=20
_<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"VALUES=20
(@FileName, @ExpireTime, @PostedBy, @HashName, @FileSize, @Description); =
" &amp;=20
_<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
"SELECT FileID, FileName, ExpireTime, PostedBy, HashName, FileSize, =
Description=20
" &amp;=20
_<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
"FROM FD_Files WHERE (FileID =3D Scope_Identity())"<BR>&nbsp;&nbsp; =
.CommandType =3D=20
CommandType.Text<BR>&nbsp;&nbsp; .Connection =3D sqlCnxn<BR>&nbsp;&nbsp; =

.Parameters.Add(New SqlParameter("@FileName", SqlDbType.VarChar, 300,=20
"FileName"))<BR>&nbsp;&nbsp; .Parameters.Add(New =
SqlParameter("@ExpireTime",=20
SqlDbType.DateTime, 4, "ExpireTime"))<BR>&nbsp;&nbsp; =
.Parameters.Add(New=20
SqlParameter("@PostedBy", SqlDbType.Char, 25, =
"PostedBy"))<BR>&nbsp;&nbsp;=20
.Parameters.Add(New SqlParameter("@HashName", SqlDbType.VarChar, 25,=20
"HashName"))<BR>&nbsp;&nbsp; .Parameters.Add(New =
SqlParameter("@FileSize",=20
SqlDbType.VarChar, 10, "FileSize"))<BR>&nbsp;&nbsp; .Parameters.Add(New=20
SqlParameter("@Description", SqlDbType.VarChar, 512, =
"Description"))<BR>End=20
With</FONT></FONT></DIV><FONT face=3DArial size=3D2><FONT =
face=3D"Courier New">
<DIV><FONT face=3DArial></FONT><FONT face=3DArial></FONT><FONT=20
face=3DArial></FONT><BR>daFiles.InsertCommand =3D =
sqlInsert<BR>daFiles.Fill(dsFiles,=20
"FD_Files")<BR></DIV>
<DIV>Dim dr As DataRow =3D dsFiles.Tables(0).NewRow<BR>Dim dFileSize As =
Double =3D=20
1234567 / 1024 / 1024<BR>If dFileSize &lt; 0.01 Then dFileSize =3D =
0.01<BR></DIV>
<DIV>dr("FileName") =3D "TestFileName"<BR>dr("ExpireTime") =3D=20
Now.AddHours(24)<BR>dr("PostedBy") =3D "JerryCa"<BR>dr("HashName") =3D=20
sFileName<BR>dr("FileSize") =3D Format(dFileSize, "N") &amp; "=20
MB"<BR>dr("Description") =3D "Test Description"<BR></DIV>
<DIV>dsFiles.Tables(0).Rows.Add(dr)<BR>daFiles.Update(dsFiles,=20
"FD_Files")<BR>dsFiles.AcceptChanges()<BR></DIV>
<DIV>Dim i As Int16<BR>&nbsp;&nbsp; For i =3D 0 To=20
dsFiles.Tables("FD_Files").Rows.Count - 1<BR>&nbsp;&nbsp; With=20
dsFiles.Tables("FD_Files")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Debug.WriteLine("FileID: " &amp; .Rows(i)(0).ToString)<BR>&nbsp;&nbsp;=20
&nbsp;&nbsp; Debug.WriteLine("FileName: " &amp;=20
.Rows(i)(1).ToString)<BR>&nbsp;&nbsp; &nbsp;&nbsp; =
Debug.WriteLine("ExpireTime:=20
" &amp; .Rows(i)(2).ToString)<BR>&nbsp;&nbsp; End With<BR>Next i</DIV>
<DIV>--------------------------------------------------------------------=
--</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial>Now, I assumed that the SELECT attached to the =
INSERT=20
would cause the updated dataset to contain only the new record because =
of the=20
WHERE clause.&nbsp; But the updated ds always has the full table.&nbsp; =
How am I=20
supposed to know which is the new record?&nbsp; Is it safe to assume =
that the=20
last record in the ds is the new one?&nbsp; According th the KB artice I =
got=20
this code from, I should see "The new record", not all records.&nbsp; =
What am I=20
missing?&nbsp; Thanks!</FONT></DIV>
<DIV><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial>Jerry</FONT></DIV>
<DIV><FONT face=3DArial></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>"Miha Markic [MVP C#]" &lt;miha at =
rthand com&gt;=20
wrote in message news:O0Cwhl59DHA.1128@tk2msftngp13.phx.gbl...<BR>&gt; =
Hi=20
Jerry,<BR>&gt; <BR>&gt; Scope_identity() is the right one.<BR>&gt; About =
your=20
problem: what do you mean that you get the whole table?<BR>&gt; What is =
your=20
scenario?<BR>&gt; -- <BR>&gt; Miha Markic [MVP C#] - RightHand .NET =
consulting=20
&amp; software development<BR>&gt; miha at rthand com<BR>&gt;=20
www.rthand.com<BR>&gt; <BR>&gt; "Jerry Camel" &lt;rlrcstr@msn.com&gt; =
wrote in=20
message<BR>&gt; news:%23taobN09DHA.1816@TK2MSFTNGP12.phx.gbl...<BR>&gt; =
&gt;=20
This has been addressed several times and I've read several articles =
and<BR>&gt;=20
&gt; posts and I still can't get this to work.&nbsp; (This is VB =
.NET)<BR>&gt;=20
&gt;<BR>&gt; &gt; My understanding is that if I append a select =
statement to=20
my<BR>&gt; DataAdapter's<BR>&gt; &gt; Insert statement that I should be =
able to=20
get an updated resultset.&nbsp; I've<BR>&gt; &gt; got this:<BR>&gt; =
&gt;<BR>&gt;=20
&gt; INSERT INTO dbo.FD_Files (FileName, ExpireTime, PostedBy, =
HashName,<BR>&gt;=20
&gt; FileSize, Description) VALUES (@FileName, @ExpireTime, =
@PostedBy,<BR>&gt;=20
@HashName,<BR>&gt; &gt; @FileSize, @Description); SELECT FileID, =
FileName,=20
ExpireTime, PostedBy,<BR>&gt; &gt; HashName, FileSize, Description FROM=20
dbo.FD_Files WHERE FileID =3D<BR>&gt; @@IDENTITY<BR>&gt; &gt;<BR>&gt; =
&gt; I've=20
also tried using SCOPE_IDENTITY() - reagrdless, I always get the<BR>&gt; =

whole<BR>&gt; &gt; table returned.<BR>&gt; &gt;<BR>&gt; &gt; I need to =
know the=20
identity of the record I just added and I keep getting<BR>&gt; a<BR>&gt; =
&gt;=20
whole table.&nbsp; It's making me crazy.<BR>&gt; &gt;<BR>&gt; &gt; I've =
even=20
tried setting the adapter's select command to SELECT FileID from<BR>&gt; =
&gt;=20
FD_Files WHERE FileID =3D SCOPE_IDENTITY() and re-filling the =
dataset.&nbsp;=20
I<BR>&gt; get<BR>&gt; &gt; the whole damned table.&nbsp; =
Why!!!!????<BR>&gt;=20
&gt;<BR>&gt; &gt; What am I missing?&nbsp; I've tried using the built in =
wizard=20
which comes up<BR>&gt; with<BR>&gt; &gt; an insert statement just like =
the one=20
above and I still end up with the<BR>&gt; &gt; whole table after I=20
update.<BR>&gt; &gt;<BR>&gt; &gt; Can anyone shed some light on =
this?&nbsp;=20
Thanks.<BR>&gt; &gt;<BR>&gt; &gt; Jerry<BR>&gt; &gt;<BR>&gt; =
&gt;<BR>&gt;=20
<BR>&gt; </FONT></DIV></BODY></HTML>

------=_NextPart_000_000C_01C3F793.FA6370E0--


Re: I Have No Identity by Miha

Miha
Fri Feb 20 08:45:05 CST 2004

Hi Jerry,

Select in insert stataments serves only for refreshing the values such as
identity when you insert new records to database (it is invoked and used
automatically within Update method).
The RowState of row indicates the well state of row.
However, this state is reset after you call AcceptChanges to Unmodified.

HTH,
--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
"Jerry Camel" <rlrcstr@msn.com> wrote in message
news:uHDtV679DHA.1392@tk2msftngp13.phx.gbl...
I've got everythign stripped down to just the basics - taking my lead from a
kb article. All I'm doing at this point is adding a single record to a
table and trying to get the identity (FileID) value back so I can add recs
to a related table. Here's my code:

----------------------------------------------------------------------
Dim sqlCnxn As New SqlConnection("user id=SomeUser;password=password;data
source=MYSERVER")
Dim cmdFiles As New SqlCommand()

Dim sFileName As String
sFileName = Now.Millisecond & Now.Second & Now.Minute & Now.Hour & Now.Year
& Now.Month & Now.Day

Dim daFiles As New SqlDataAdapter("SELECT FileID, FileName, ExpireTime,
PostedBy, HashName, FileSize, Description FROM FD_Files", sqlCnxn)

Dim dsFiles As New DataSet()
Dim sqlInsert As New SqlCommand()

With sqlInsert
.CommandText = "INSERT INTO FD_Files (FileName, ExpireTime, PostedBy,
HashName, FileSize, Description) " & _
"VALUES (@FileName, @ExpireTime, @PostedBy, @HashName,
@FileSize, @Description); " & _
"SELECT FileID, FileName, ExpireTime, PostedBy, HashName,
FileSize, Description " & _
"FROM FD_Files WHERE (FileID = Scope_Identity())"
.CommandType = CommandType.Text
.Connection = sqlCnxn
.Parameters.Add(New SqlParameter("@FileName", SqlDbType.VarChar, 300,
"FileName"))
.Parameters.Add(New SqlParameter("@ExpireTime", SqlDbType.DateTime, 4,
"ExpireTime"))
.Parameters.Add(New SqlParameter("@PostedBy", SqlDbType.Char, 25,
"PostedBy"))
.Parameters.Add(New SqlParameter("@HashName", SqlDbType.VarChar, 25,
"HashName"))
.Parameters.Add(New SqlParameter("@FileSize", SqlDbType.VarChar, 10,
"FileSize"))
.Parameters.Add(New SqlParameter("@Description", SqlDbType.VarChar, 512,
"Description"))
End With

daFiles.InsertCommand = sqlInsert
daFiles.Fill(dsFiles, "FD_Files")

Dim dr As DataRow = dsFiles.Tables(0).NewRow
Dim dFileSize As Double = 1234567 / 1024 / 1024
If dFileSize < 0.01 Then dFileSize = 0.01

dr("FileName") = "TestFileName"
dr("ExpireTime") = Now.AddHours(24)
dr("PostedBy") = "JerryCa"
dr("HashName") = sFileName
dr("FileSize") = Format(dFileSize, "N") & " MB"
dr("Description") = "Test Description"

dsFiles.Tables(0).Rows.Add(dr)
daFiles.Update(dsFiles, "FD_Files")
dsFiles.AcceptChanges()

Dim i As Int16
For i = 0 To dsFiles.Tables("FD_Files").Rows.Count - 1
With dsFiles.Tables("FD_Files")
Debug.WriteLine("FileID: " & .Rows(i)(0).ToString)
Debug.WriteLine("FileName: " & .Rows(i)(1).ToString)
Debug.WriteLine("ExpireTime: " & .Rows(i)(2).ToString)
End With
Next i
----------------------------------------------------------------------

Now, I assumed that the SELECT attached to the INSERT would cause the
updated dataset to contain only the new record because of the WHERE clause.
But the updated ds always has the full table. How am I supposed to know
which is the new record? Is it safe to assume that the last record in the
ds is the new one? According th the KB artice I got this code from, I
should see "The new record", not all records. What am I missing? Thanks!

Jerry


"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:O0Cwhl59DHA.1128@tk2msftngp13.phx.gbl...
> Hi Jerry,
>
> Scope_identity() is the right one.
> About your problem: what do you mean that you get the whole table?
> What is your scenario?
> --
> Miha Markic [MVP C#] - RightHand .NET consulting & software development
> miha at rthand com
> www.rthand.com
>
> "Jerry Camel" <rlrcstr@msn.com> wrote in message
> news:%23taobN09DHA.1816@TK2MSFTNGP12.phx.gbl...
> > This has been addressed several times and I've read several articles and
> > posts and I still can't get this to work. (This is VB .NET)
> >
> > My understanding is that if I append a select statement to my
> DataAdapter's
> > Insert statement that I should be able to get an updated resultset.
I've
> > got this:
> >
> > INSERT INTO dbo.FD_Files (FileName, ExpireTime, PostedBy, HashName,
> > FileSize, Description) VALUES (@FileName, @ExpireTime, @PostedBy,
> @HashName,
> > @FileSize, @Description); SELECT FileID, FileName, ExpireTime, PostedBy,
> > HashName, FileSize, Description FROM dbo.FD_Files WHERE FileID =
> @@IDENTITY
> >
> > I've also tried using SCOPE_IDENTITY() - reagrdless, I always get the
> whole
> > table returned.
> >
> > I need to know the identity of the record I just added and I keep
getting
> a
> > whole table. It's making me crazy.
> >
> > I've even tried setting the adapter's select command to SELECT FileID
from
> > FD_Files WHERE FileID = SCOPE_IDENTITY() and re-filling the dataset. I
> get
> > the whole damned table. Why!!!!????
> >
> > What am I missing? I've tried using the built in wizard which comes up
> with
> > an insert statement just like the one above and I still end up with the
> > whole table after I update.
> >
> > Can anyone shed some light on this? Thanks.
> >
> > Jerry
> >
> >
>
>



Re: I Have No Identity by Jerry

Jerry
Fri Feb 20 09:19:20 CST 2004

I'm such a moron... I've been looking for the result in the dataset instead
of the datarow object that I added... Now I've got it.
Also, it looks like I don't need the call to AcceptChanges as the rowstate
is Unchanged after the call to Update.

The KB article is extremely misleading.

Thanks

Jerry

"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:eOdRfA89DHA.1816@TK2MSFTNGP12.phx.gbl...
> Hi Jerry,
>
> Select in insert stataments serves only for refreshing the values such as
> identity when you insert new records to database (it is invoked and used
> automatically within Update method).
> The RowState of row indicates the well state of row.
> However, this state is reset after you call AcceptChanges to Unmodified.
>
> HTH,
> --
> Miha Markic [MVP C#] - RightHand .NET consulting & software development
> miha at rthand com
> www.rthand.com
> "Jerry Camel" <rlrcstr@msn.com> wrote in message
> news:uHDtV679DHA.1392@tk2msftngp13.phx.gbl...
> I've got everythign stripped down to just the basics - taking my lead from
a
> kb article. All I'm doing at this point is adding a single record to a
> table and trying to get the identity (FileID) value back so I can add recs
> to a related table. Here's my code:
>
> ----------------------------------------------------------------------
> Dim sqlCnxn As New SqlConnection("user id=SomeUser;password=password;data
> source=MYSERVER")
> Dim cmdFiles As New SqlCommand()
>
> Dim sFileName As String
> sFileName = Now.Millisecond & Now.Second & Now.Minute & Now.Hour &
Now.Year
> & Now.Month & Now.Day
>
> Dim daFiles As New SqlDataAdapter("SELECT FileID, FileName, ExpireTime,
> PostedBy, HashName, FileSize, Description FROM FD_Files", sqlCnxn)
>
> Dim dsFiles As New DataSet()
> Dim sqlInsert As New SqlCommand()
>
> With sqlInsert
> .CommandText = "INSERT INTO FD_Files (FileName, ExpireTime, PostedBy,
> HashName, FileSize, Description) " & _
> "VALUES (@FileName, @ExpireTime, @PostedBy, @HashName,
> @FileSize, @Description); " & _
> "SELECT FileID, FileName, ExpireTime, PostedBy,
HashName,
> FileSize, Description " & _
> "FROM FD_Files WHERE (FileID = Scope_Identity())"
> .CommandType = CommandType.Text
> .Connection = sqlCnxn
> .Parameters.Add(New SqlParameter("@FileName", SqlDbType.VarChar, 300,
> "FileName"))
> .Parameters.Add(New SqlParameter("@ExpireTime", SqlDbType.DateTime, 4,
> "ExpireTime"))
> .Parameters.Add(New SqlParameter("@PostedBy", SqlDbType.Char, 25,
> "PostedBy"))
> .Parameters.Add(New SqlParameter("@HashName", SqlDbType.VarChar, 25,
> "HashName"))
> .Parameters.Add(New SqlParameter("@FileSize", SqlDbType.VarChar, 10,
> "FileSize"))
> .Parameters.Add(New SqlParameter("@Description", SqlDbType.VarChar,
512,
> "Description"))
> End With
>
> daFiles.InsertCommand = sqlInsert
> daFiles.Fill(dsFiles, "FD_Files")
>
> Dim dr As DataRow = dsFiles.Tables(0).NewRow
> Dim dFileSize As Double = 1234567 / 1024 / 1024
> If dFileSize < 0.01 Then dFileSize = 0.01
>
> dr("FileName") = "TestFileName"
> dr("ExpireTime") = Now.AddHours(24)
> dr("PostedBy") = "JerryCa"
> dr("HashName") = sFileName
> dr("FileSize") = Format(dFileSize, "N") & " MB"
> dr("Description") = "Test Description"
>
> dsFiles.Tables(0).Rows.Add(dr)
> daFiles.Update(dsFiles, "FD_Files")
> dsFiles.AcceptChanges()
>
> Dim i As Int16
> For i = 0 To dsFiles.Tables("FD_Files").Rows.Count - 1
> With dsFiles.Tables("FD_Files")
> Debug.WriteLine("FileID: " & .Rows(i)(0).ToString)
> Debug.WriteLine("FileName: " & .Rows(i)(1).ToString)
> Debug.WriteLine("ExpireTime: " & .Rows(i)(2).ToString)
> End With
> Next i
> ----------------------------------------------------------------------
>
> Now, I assumed that the SELECT attached to the INSERT would cause the
> updated dataset to contain only the new record because of the WHERE
clause.
> But the updated ds always has the full table. How am I supposed to know
> which is the new record? Is it safe to assume that the last record in the
> ds is the new one? According th the KB artice I got this code from, I
> should see "The new record", not all records. What am I missing? Thanks!
>
> Jerry
>
>
> "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
> news:O0Cwhl59DHA.1128@tk2msftngp13.phx.gbl...
> > Hi Jerry,
> >
> > Scope_identity() is the right one.
> > About your problem: what do you mean that you get the whole table?
> > What is your scenario?
> > --
> > Miha Markic [MVP C#] - RightHand .NET consulting & software development
> > miha at rthand com
> > www.rthand.com
> >
> > "Jerry Camel" <rlrcstr@msn.com> wrote in message
> > news:%23taobN09DHA.1816@TK2MSFTNGP12.phx.gbl...
> > > This has been addressed several times and I've read several articles
and
> > > posts and I still can't get this to work. (This is VB .NET)
> > >
> > > My understanding is that if I append a select statement to my
> > DataAdapter's
> > > Insert statement that I should be able to get an updated resultset.
> I've
> > > got this:
> > >
> > > INSERT INTO dbo.FD_Files (FileName, ExpireTime, PostedBy, HashName,
> > > FileSize, Description) VALUES (@FileName, @ExpireTime, @PostedBy,
> > @HashName,
> > > @FileSize, @Description); SELECT FileID, FileName, ExpireTime,
PostedBy,
> > > HashName, FileSize, Description FROM dbo.FD_Files WHERE FileID =
> > @@IDENTITY
> > >
> > > I've also tried using SCOPE_IDENTITY() - reagrdless, I always get the
> > whole
> > > table returned.
> > >
> > > I need to know the identity of the record I just added and I keep
> getting
> > a
> > > whole table. It's making me crazy.
> > >
> > > I've even tried setting the adapter's select command to SELECT FileID
> from
> > > FD_Files WHERE FileID = SCOPE_IDENTITY() and re-filling the dataset.
I
> > get
> > > the whole damned table. Why!!!!????
> > >
> > > What am I missing? I've tried using the built in wizard which comes
up
> > with
> > > an insert statement just like the one above and I still end up with
the
> > > whole table after I update.
> > >
> > > Can anyone shed some light on this? Thanks.
> > >
> > > Jerry
> > >
> > >
> >
> >
>
>



Re: I Have No Identity by Miha

Miha
Fri Feb 20 09:39:27 CST 2004


> Also, it looks like I don't need the call to AcceptChanges as the rowstate
> is Unchanged after the call to Update.

Yup, Update does it if the update is successful.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com