473,761 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new to vb - is this code ok ? - retrieving ID from SQL stored procedure

i've been playing with this code to return an ID on an insert - it seems a
little dirty/hacky to me

stored proc goes something like this (ive stripped out uneccesary sql code -
the proc works fine) :-

CREATE PROCEDURE uipersonal

declare all the variables here

AS

IF EXISTS (SELECT * database WHERE id=@mainid)

UPDATE

ELSE

INSERT

SELECT SCOPE_IDENTITY( )

GO

thats basically the stored proc
my vb code is something like :-

Private Sub putdatain()

mainid = TextBox1.Text.T oString
If mainid = "" Then mainid = "0"

Dim conn As New System.Data.Sql Client.SqlConne ction(SQLstrCon n)
Dim sql As String = "uipersonal '" all the variables i send
Dim Cmd As New System.Data.Sql Client.SqlComma nd(sql, conn)
Dim objDR As System.Data.Sql Client.SqlDataR eader

conn.Open()

If mainid = "0" Then
TextBox1.Text = Cmd.ExecuteScal ar
ElseIf mainid > "0" Then
objDR =
Cmd.ExecuteRead er(System.Data. CommandBehavior .CloseConnectio n)
End If

conn.Close()

End Sub

this works ok but seems a little messy - and i was wondering if this method
would cause issues in the future
(atm its just test code to play around)

thanks

mark
Nov 21 '05 #1
7 1224
Luna,

Somewhere I miss the setting in your code the setting of the parameter
http://msdn.microsoft.com/library/de...erstopic.aspAs well do I miss where you tell that the command is a stored procedurehttp://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfsystemdata sqlclientsqlcom mandclasscomman dtypetopic.aspI hope this helps?Cor

Nov 21 '05 #2
Luna,

Somewhere I miss in your code the setting of the parameter
<>

http://msdn.microsoft.com/library/de...eterstopic.asp

<>
As well do I miss where you tell that the command is a stored procedure

<>

http://msdn.microsoft.com/library/de...dtypetopic.asp

<>

I hope this helps?

Cor
Nov 21 '05 #3

"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Luna,

Somewhere I miss in your code the setting of the parameter
<>

http://msdn.microsoft.com/library/de...eterstopic.asp Dim conn As New System.Data.Sql Client.SqlConne ction(SQLstrCon n)

sqlstrconn is a variable that just contains a standard connection string

As well do I miss where you tell that the command is a stored procedure
Dim sql As String = "uipersonal '"
is the stored procedure (i missed out all the variables as theres a few)

http://msdn.microsoft.com/library/de...dtypetopic.asp
<>

I hope this helps?

Cor


the code itself works fine - i just wasnt sure if it was good code or not -
seems a bit hacky to get the new @@identity out of the stored procedure -
just wondered if i was doing it right!

mark
Nov 21 '05 #4
Luna,

As usual do I not give answers on security. When you don't use integrated
security, than it i thought that it was the next step as you do it. (I wrote
thought).

However for this I would not only rely for answers for this in this
newsgroup, however on more answers in the newsgroups

microsoft.publi c.dotnet.framew ork.adonet
microsoft.publi c.dotnet.securi ty

I hope that it helps anyway something

Cor
Nov 21 '05 #5
thanks , ill try a post in adonet

cheers

mark


Nov 21 '05 #6
Yes, this works, although I don't know if you need to create the reader for
the update condition. You could, as easily, use ExecuteNonQuery and check
to make sure that you updated one record.

These days, whenever I can, I avoid using identity columns because of
problems like this. I use GUIDs instead that are generated in the
application. (e.g. if the app is creating a new record, the app calls
Guid.NewGuid to create a new Guid and passes that in. The proc still needs
to look for update vs insert, but it never needs to return the new key
value, because the app already has it.

Anyway, your code looks fine.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"luna_s" <lu**@moonbasea lpha.com> wrote in message
news:BM******** ******@newsfe3-win.ntli.net...
i've been playing with this code to return an ID on an insert - it seems a
little dirty/hacky to me

stored proc goes something like this (ive stripped out uneccesary sql code - the proc works fine) :-

CREATE PROCEDURE uipersonal

declare all the variables here

AS

IF EXISTS (SELECT * database WHERE id=@mainid)

UPDATE

ELSE

INSERT

SELECT SCOPE_IDENTITY( )

GO

thats basically the stored proc
my vb code is something like :-

Private Sub putdatain()

mainid = TextBox1.Text.T oString
If mainid = "" Then mainid = "0"

Dim conn As New System.Data.Sql Client.SqlConne ction(SQLstrCon n)
Dim sql As String = "uipersonal '" all the variables i send
Dim Cmd As New System.Data.Sql Client.SqlComma nd(sql, conn)
Dim objDR As System.Data.Sql Client.SqlDataR eader

conn.Open()

If mainid = "0" Then
TextBox1.Text = Cmd.ExecuteScal ar
ElseIf mainid > "0" Then
objDR =
Cmd.ExecuteRead er(System.Data. CommandBehavior .CloseConnectio n)
End If

conn.Close()

End Sub

this works ok but seems a little messy - and i was wondering if this method would cause issues in the future
(atm its just test code to play around)

thanks

mark

Nov 21 '05 #7

"Nick Malik [Microsoft]" <ni*******@hotm ail.nospam.com> wrote in message
news:UZ******** ************@co mcast.com...
Yes, this works, although I don't know if you need to create the reader for the update condition. You could, as easily, use ExecuteNonQuery and check
to make sure that you updated one record.

These days, whenever I can, I avoid using identity columns because of
problems like this. I use GUIDs instead that are generated in the
application. (e.g. if the app is creating a new record, the app calls
Guid.NewGuid to create a new Guid and passes that in. The proc still needs to look for update vs insert, but it never needs to return the new key
value, because the app already has it.

Anyway, your code looks fine.


thanks for easing my mind !, ill look into the guid thing,
mark
Nov 21 '05 #8

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

Similar topics

1
9491
by: PinkGuava | last post by:
Hi, I have a T-SQL stored procedure that returns both output parameters and a recordset. How do I retrieve them in my ASP script? As far as I know, the ADO Command object can be used to retrieve the output parameters, but will I be able to retrieve the recordset using the Command object as well? Or do I have to use the Recordset object? Example of stored procedure:
1
30743
by: Anthony Robinson | last post by:
I'm executing a stored procedure and getting the SQL0440N No authorized routine named "AMROBI2.CREATEAIMCONNECTION" of type "PROCEDURE " having compatible arguments was found. The schema exists, the stored procedure exists, the user executing the procedure has execute permissions on the procedure and also has insert, update, delete rights on the table being accessed in the stored procedure. I'm sending in the correct number of input...
4
3190
by: Rhino | last post by:
Is it possible for a Java Stored Procedure in DB2 V7.2 (Windows) to pass a Throwable back to the calling program as an OUT parameter? If yes, what datatype should I use when registering the Throwable as an OUT parameter and what datatype should I use in the CREATE PROCEDURE and DROP PROCEDURE statements? Here's what I tried: - the method signature for the stored procedure included: Throwable throwable
6
6767
by: Wojciech Wendrychowicz | last post by:
Hello to All, I'm trying to retrieve records from AS/400 in an VBA application. So, I've made an RPG program, then a stored procedure wchich calls that RPG program, and finally some VBA code to call the stored procedure and retrieve data from AS/400. The problem is, that when I finally run my VB code, it just hangs. But when I call the same stored procedure from "pure" SQL - it works perfect. (I evaluate Aqua Data Studio 3.7) What I...
5
1727
by: Andy G | last post by:
I have a registration page that captures 75% of the users data. After they enter that info they are redirected to one of two pages depending on how they answered a question on the registation page. My question is would it be better to store all of the fields(15 or so) from the first page in session variables and reference them from the next page and run ONE stored procedure OR would it be better to run a stored procedure the first page...
6
2270
by: Stu Lock | last post by:
Hi, I have a stored procedure: --/ snip /-- CREATE PROCEDURE sp_AddEditUsers ( @Users_ID int, @UserName nvarchar(80), @Password nvarchar(80),
15
3535
by: gunnar.sigurjonsson | last post by:
I´m having some problem retrieving identity value from my newly inserted row into a view. I have two tables T1 and T2 which I define as following CREATE TABLE T1 ( id BIGINT GENERATED ALWAYS AS IDENTITY ( START WITH 1
28
1854
by: Joey Martin | last post by:
One of my servers got hacked with the SQL injection due to poor coding. So, I had someone write a stored procedure and new code. But, to me, it looks just as flawed, even using the stored procedure. email=request("email") password=request("pw") OLD CODE: sql="select * from tablename where email='" & email & "' and password='"
4
1655
by: =?ISO-8859-15?Q?Jan_L=F6hndorf?= | last post by:
Hi. Is it possible to retrieve the text I printed in a stored procedure using PRINT command when using C# SqlDataReader? Any idea? Thanks for every hint, Jan
0
9377
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
9989
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
9925
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
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
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...
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.