Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 10th, 2010, 03:15 PM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default Stored Procedure Not Returning Same Value As Inline Sql VB.Net 2008

Hi, I am attempting to pass a parameter into a Sub. I have the following:
  • conn = connection string
  • dt = datatable
  • sqlCmd = sql command
  • sqlDa = sqlDataAdapter(sqlcmd)
Pertinent Code:

Code:
Sub getInfo(byVal txtName) 'where txtName is a value passed from a textbox on frmOther
 
Dim sqlCmd As New SqlCommand("Exec myProcedure ", Conn)
 
sqlCmd.Parameters.AddWithValue("@myName", "txtName")
Dim sqlDa As New SqlDataAdapter(sqlCmd)
 
sqlDa.Fill(dt)
 
frmOther.docTableAdapter.Fill(frmOther.DataSet.doc, txtName.ToString)
'Set form objects to returned values
frmOther.txtName.Text = dt.Rows(i)("Name").ToString
 
End Sub
Here is the Stored Procedure (pertinent info):

Code:
@myName AS varchar(50) = " "
 
AS
BEGIN
 
SELECT blah, blah
FROM mytable s,
WHERE s.tName = @myName
 
END
The EXACT same code (I'm really using) will work if my sqlCmd contains the actual sql code instead of the stored procedure. The only difference is that I am hard coding s.tName = 'someName' instead of passing it as a parameter. I am assuming my problem lies where I am passing the parameter to the stored procedure but everything I have tried (to this point) has failed. Any help is truly appreciated!

Thanks
__________________
SLBIBS
 
Old March 10th, 2010, 03:43 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Take a look at this:

sqlCmd.Parameters.AddWithValue("@myName", "txtName")

You're not passing the value of txtName, but passing it as a literal...

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old March 10th, 2010, 03:58 PM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks Imar for the quick reply! I had actually tried that previously but to no avail as it returns the same "wrong" record whether or not I am passing a literal or value of. It is currently set to pass the value of per your suggestion.

I am connecting to the database (using the stored procedure) but it is not finding the appropriate record(s).

However if i execute the Sproc within Management Studio, it returns that actual records on the execution but it also gives me the following:

Code:
SELECT 'Return Value' = @return_value
 
And the resulting value of this is 0
I have not set @return_value anywhere in my Sproc...could this be a culprit? I can try to parse out confidential information and post the actual Sproc (if this would help) but again everything works on its own but if I try to pass the parameter (calling the Sproc via the Exec), I do not get back what i was expecting.
__________________
SLBIBS

Last edited by slbibs; March 10th, 2010 at 04:02 PM..
 
Old March 10th, 2010, 04:20 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

And what record does it return? Does it return any data at all?

There are a few issues with your code that make it hard to track this down, especially for me. Try fixing that before you move on:

1. Strongly type your method parameters. What is txtName? Is it a string? Then make it explicit:

Sub getInfo(ByVal txtName As String)

That way, you can be sure you're not passing an entire TextBox which, using ToString, results in the literal TextBox instead of the actual value.

2. Don't use Exec. AFAIK, you don't need it.

3. What is i? Does it have the correct value?

Once you fix these issues, try turning on the SQL Profiler and see what gets sent to the database. Alternatively, select the input parameter:

SELECT @txtName FROM SomeTable

so you can what you're passing to it.

Debug the code and see what is being passed and what is being returned.

Also, this may help:http://msdn.microsoft.com/en-us/libr...y8(VS.71).aspx

And stored procedures have implicit return values if you don't specify them.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; March 10th, 2010 at 04:21 PM.. Reason: Added note about return values
The Following User Says Thank You to Imar For This Useful Post:
slbibs (March 11th, 2010)
 
Old March 10th, 2010, 04:49 PM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Imar,

These are excellent suggestions. I will try them out and if i find the culprit, I will make a notation of it in this thread.

Thanks for your time in this matter.
__________________
SLBIBS
 
Old March 10th, 2010, 06:17 PM
Authorized User
 
Join Date: Jun 2003
Posts: 22
Thanks: 1
Thanked 0 Times in 0 Posts
Default

A brief update... it is now working as it should! I am not sure what went wrong (exactly) but I wanted to touch on the following:

I made my byVal (explicit)
Removed Exec from procedure call
i - is the number of rows returned from the query

When that didn't work, I went back to the drawing board to see what was wrong and the culprit ended up being my dataset Fill. Apparently during my haste to get the job done, I configured the Fill to look at the wrong Stored Procedure. That was why it was connecting but not returning what I was anticipating. Once I corrected that (and cleaned up my code, it worked as expected).

Thanks again for your help! You are a very good troubleshooter
__________________
SLBIBS
 
Old March 11th, 2010, 04:16 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning a value (stored procedure scandalous ASP.NET 2.0 Basics 9 November 7th, 2007 03:41 PM
VB & SQL stored procedure garaxan VB How-To 1 September 4th, 2007 11:08 AM
Returning Values from a Stored Procedure kadjw SQL Server ASP 1 September 13th, 2006 12:08 PM
Calling Stored procedure in VB.NET kganti VB.NET 2002/2003 Basics 0 March 26th, 2006 08:30 PM
Problems returning count in Stored Procedure planza SQL Language 1 December 21st, 2005 03:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.