473,473 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create 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.ToString
If mainid = "" Then mainid = "0"

Dim conn As New System.Data.SqlClient.SqlConnection(SQLstrConn)
Dim sql As String = "uipersonal '" all the variables i send
Dim Cmd As New System.Data.SqlClient.SqlCommand(sql, conn)
Dim objDR As System.Data.SqlClient.SqlDataReader

conn.Open()

If mainid = "0" Then
TextBox1.Text = Cmd.ExecuteScalar
ElseIf mainid > "0" Then
objDR =
Cmd.ExecuteReader(System.Data.CommandBehavior.Clos eConnection)
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 1212
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/frlrfsystemdatasqlclientsqlcommandclasscommandtype topic.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****************@TK2MSFTNGP15.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.SqlClient.SqlConnection(SQLstrConn)

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.public.dotnet.framework.adonet
microsoft.public.dotnet.security

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**@moonbasealpha.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.ToString
If mainid = "" Then mainid = "0"

Dim conn As New System.Data.SqlClient.SqlConnection(SQLstrConn)
Dim sql As String = "uipersonal '" all the variables i send
Dim Cmd As New System.Data.SqlClient.SqlCommand(sql, conn)
Dim objDR As System.Data.SqlClient.SqlDataReader

conn.Open()

If mainid = "0" Then
TextBox1.Text = Cmd.ExecuteScalar
ElseIf mainid > "0" Then
objDR =
Cmd.ExecuteReader(System.Data.CommandBehavior.Clos eConnection)
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*******@hotmail.nospam.com> wrote in message
news:UZ********************@comcast.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
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...
1
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...
4
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...
6
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...
5
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...
6
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
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...
28
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...
4
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
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,...
0
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...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.