473,804 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert a record with a stored procecure

Hello,

My insert results in two records - or better stated the temporary
record (with the temporary ID value of -1) stays as the stored
procedure returns the new record with its data base assigned ID value

Below is shown the ID field and one data field. Notice the last two
records shown.
1 bat
2 frog
3 wren
-1 cat < this is the lingering temporary record
4 cat < this is the actual new row

When I close and reopen the form the temporary record will go away.

(I use GUIDs for my Id's so am not familar with auto-increment
syntax...)

Below is my code. Can you tell me what I am missing?

Here is my insert stored procedure;
\\
CREATE PROCEDURE dbo.usp_102Phas e_ins(
@Phase varchar(50),
@Ord tinyint,
@Hide bit
) AS
SET NOCOUNT OFF;
INSERT INTO lkp102Phase(
Phase,
Ord,
Hide
) VALUES (
@Phase,
@Ord,
@Hide
);
SELECT
pkPhaseId,
Phase,
Ord,
Hide
FROM lkp102Phase

WHERE
(pkPhaseID = @@Identity)
//

Below is the code behind my update button;
\\
Sub btnEndEdit_Clic k(ByVal sender As System.Object, _
ByVal e As System.EventArg s) Handles btnEndEdit.Clic k

'Save changes to to the local dataset
_bmb.EndCurrent Edit()
Call UpdateSource2(D ALa.da102Phase, "lkp102Phas e")

End sub

Protected Sub UpdateSource2(B yVal dA As SqlDataAdapter, _
ByVal tbl As String)

Dim dsDataChanges As New CLIP.dsTables
dsDataChanges = CType(_dataSet1 .GetChanges, CLIP.dsTables)
If (Not (dsDataChanges) Is Nothing) Then
Try
dA.Update(dsDat aChanges, tbl)
_dataSet1.Merge (dsDataChanges)
_dataSet1.Accep tChanges()
Catch ...
End Try
End If
End Sub
//

Here is the DataAccess code - Command parameter
\\
With cmd102Phase_Ins
.CommandType = CommandType.Sto redProcedure
.CommandText = "usp_102Phase_i ns"
.Connection = sqlConn
With cmd102Phase_Ins .Parameters
.Add(New SqlParameter("@ RETURN_VALUE", SqlDbType.Int, 4, _
ParameterDirect ion.ReturnValue , False, CType(0, Byte), _
CType(0, Byte), "", DataRowVersion. Current, Nothing))

''.Add(New SqlParameter("@ pkPhaseId", SqlDbType.Int, 4, "pkPhaseId" ))
.Add(New SqlParameter("@ Phase", SqlDbType.VarCh ar, 50, "Phase"))
.Add(New SqlParameter("@ Ord", SqlDbType.TinyI nt, 1, "Ord"))
.Add(New SqlParameter("@ Hide", SqlDbType.Bit, 1, "Hide"))
End With
End With
//

Here is the table;
\\
CREATE TABLE [lkp102Phase] (
[pkPhaseId] [int] IDENTITY (1, 1) NOT NULL ,
[Phase] [varchar] (50) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NOT NULL ,
[Ord] [tinyint] NOT NULL CONSTRAINT [DF_lkp102Phase_ ord] DEFAULT (0),
[Hide] [bit] NOT NULL CONSTRAINT [DF_lkp102Phase_ hide] DEFAULT (0),
CONSTRAINT [PK_lkp102Phase] PRIMARY KEY CLUSTERED
(
[pkPhaseId]
) ON [PRIMARY]
) ON [PRIMARY]
GO
//

What am I missing that I causes the temporary record to remain in the
datagrid?

Thank you,
dbuchanan

Nov 21 '05 #1
2 1219
Hello,

I discovered that it depends on how the update is called. Following are
two examples #1 works and #2 does not

1.) The data adapter directly updates the dataset
\\
DALa.da101PortS ize.Update(_dat aset1, "lkp101PortSize ")
//

2.) A special dataset is created and populated with the records that
canged in the dataset. Then the update is made from that special
dataset. Then the changes are merged and accepted back into the
dataset.
\\
UpdateSource2(D ALa.da101PortSi ze, "lkp101PortSize ")

Protected Sub UpdateSource2(B yVal dA As SqlDataAdapter, ByVal tbl As
String)
Dim dsDataChanges As New CLIP.dsTables

dsDataChanges = CType(_dataSet1 .GetChanges, CLIP.dsTables)
dA.Update(dsDat aChanges, tbl)
_dataSet1.Merge (dsDataChanges)
_dataSet1.Accep tChanges()

End Sub
//

Perhaps there is a way to handle those changes so that the temporaty
record is removed. Does anyone understand how to do that?

Thank you,
dbuchanan

Nov 21 '05 #2
Hi,

"dbuchanan" <db*********@ho tmail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Hello,

I discovered that it depends on how the update is called. Following are
two examples #1 works and #2 does not

1.) The data adapter directly updates the dataset
\\
DALa.da101PortS ize.Update(_dat aset1, "lkp101PortSize ")
//

2.) A special dataset is created and populated with the records that
canged in the dataset. Then the update is made from that special
dataset. Then the changes are merged and accepted back into the
dataset.
\\
UpdateSource2(D ALa.da101PortSi ze, "lkp101PortSize ")

Protected Sub UpdateSource2(B yVal dA As SqlDataAdapter, ByVal tbl As
String)
Dim dsDataChanges As New CLIP.dsTables

dsDataChanges = CType(_dataSet1 .GetChanges, CLIP.dsTables)
dA.Update(dsDat aChanges, tbl)
_dataSet1.Merge (dsDataChanges)
_dataSet1.Accep tChanges()

End Sub
//

Perhaps there is a way to handle those changes so that the temporaty
record is removed. Does anyone understand how to do that?
See http://support.microsoft.com/default...b;en-us;313540 .

If you want to understand what is going wrong, then you need to know the
steps the DataAdapter performs when it updates the DataBase and also that
each row has three values for each field (old, new & temp).

This is how the row looks when it comes from GetChanges:
[ RowState = Inserted, NewKey = -1 ]

Once the adapter inserts the row in the DataBase then AcceptChanges is
called on that row, so the row looks like:
[ RowState = Unchanged, OldKey = -1, NewKey = -1 ]

Then the adapter fetches the new key:
[ RowState = Modified, OldKey = -1, NewKey = 5 ]

But now it goes wrong, the DataAdapter will call AcceptChanges again:
[ RowState = Unchanged, OldKey = 5, NewKey = 5 ]

At this point you *lost* the old key and mergings fails with duplicate rows
since keys are used to match.

The solution is to add an event to DataAdapter.Row Updated (not Updating) and
then set Action to skipcurrentrow for inserted rows, this will make it so
that the second AcceptChanges is not called, so your row keeps the old and
new key and it can merge. After the merging you have to call
AccceptChanges.
HTH,
Greetings

Thank you,
dbuchanan

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1802
by: | last post by:
Hi I am using SQL server 7.0 and here is my code: Dim conn set conn = Server.CreateObject("ADODB.Connection") conn.open "websql" If conn.state=adStateOpen then Response.Write("ConnMade : " & conn.state
3
2450
by: Suzanne | last post by:
Hi All I'm having problems getting my data adapter to throw a concurrency exception with an INSERT command. I want to throw a concurrency exception if an attempt is made to enter a row into tb_table when a row with the same int_UID already exists in there. Here is my stored procedure: if not exists (select int_UID from tb_table where int_UID = @aint_UID)
10
2643
by: jaYPee | last post by:
as of now i am using a stored procedure from my sql server 2000 to insert record from another table. what i need now is on how can i insert record by not using the stored procedure and insert it using dataset. here is my code in stored procedure.. CREATE PROCEDURE AddRegularLoad @SchYrSemID as int, @ProgramID as int, @Sem as varchar(50), @Year as
6
2366
by: SandySears | last post by:
I am trying to use a stored procedure to insert a record using VS 2005, VB and SQL Server Express. The code runs without errors or exceptions, and returns the new identifer in the output parameters. It returns my error text message in another output parameter as "ok", which is the value that is set in the stored procedure prior to doing the insert. It returns my var for @@rowcount as 1. However, the record does not get into the table. ...
2
2946
by: Roger | last post by:
I have a stored procedure running on DB2 V7 Z/os calling a COBOL program to do some inserts. The stored procedure have 3 input columns and one column is of varchar(32648) The stored procedure is being called from a V7 DB2 connect client. The stored procedure is giving SQL0104N An unexpected token was found if my varchar data goes beyond 1024 bytes. Anything under 1025 bytes on that column is working perfectly. Does anybody know of...
0
1417
by: Roger | last post by:
have a stored procedure running on DB2 V7 Z/os calling a COBOL program to do some inserts. The stored procedure have 3 input columns and one column is of varchar(32648) The stored procedure is being called from a V7 DB2 connect client. The stored procedure is giving SQL0104N An unexpected token was found if my varchar data goes beyond 1024 bytes. Anything under 1025 bytes on that column is working perfectly. Does anybody know of any...
0
1599
by: =?Utf-8?B?UGF1bCBCdXp6YSwgb2xkc3RlciB1c2luZyBuZXcg | last post by:
I'm trying to retrieve the @@identity value of the just-inserted record using a FormView control on an .aspx page. Here's what I have tried..... --change in-line Insert in the SqlDataSource from Text to Stored Procedure --added Insert parameter "AdrID" direction:output to the SqlDataSource --added "AdrId" as an OUT parameter in the Stored Procedure --try to retrieve the "AdrId" in the Inserted Event handler of the SqlDataSource The new...
4
3379
by: aCe | last post by:
hi all, i need to convert these simple PHP code into stored procedure : <?php $result = mssql_query( "SELECT whid, whcode FROM warehouse" ); while( $wh = mssql_fetch_object( $result ) ) { $result = mssql_query( "SELECT plid, nopl FROM packlist WHERE whid = '" . $wh->whid . "'"; while( $pl = mssql_fetch_object( $result ) ) {
4
2189
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and not reinsert it with a different revision number. Compounding the issue, we’ve also got an associated table storing properties for our entities which is not revisioned, but we still want changes to the children of our entity (additions, changes...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.