Solved

Stored Procedure Results

Posted on 2011-09-20
15
224 Views
Last Modified: 2012-06-27
I have a stored procedure that when I execute from the Stored Procedure it returns no results but if I copy and paste it into a query it does return results.  I'm a noobie so I know it is something silly,but I am unable to see it.  Thanks!
ALTER PROCEDURE dbo.ReportTypeSelect( @ReportTypeKey int)

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	SELECT ReportTypeKey, 
		NULL AS ReportTypeKey, 
		ReportTypeName,
		ReportTypeDesc,
		rt.ReportTypeGroupKey,
		ReportTypeGroupName,
		rt.OrderTypeKey,
		OrderTypeName
	FROM dbo.ReportType rt JOIN dbo.OrderType o ON rt.OrderTypeKey = o.OrderTypeKey
	JOIN dbo.ReportTypeGroup rtg ON rt.ReportTypeGroupKey = rtg.ReportTypeGroupKey
	WHERE ReportTypeKey = @ReportTypeKey
	
END

Open in new window

0
Comment
Question by:rcowen00
  • 8
  • 6
15 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36568780
When you copy-and-paste, are you including the WHERE condition? If so, are you using the same parameter as when you call the procedure? Please show HOW you are calling the procedure here, might just be an issue with the format.
0
 

Author Comment

by:rcowen00
ID: 36568802
Here it is
Private Sub LoadReportGroup()
        ReportGroup.DataSource = Nothing
        ReportGroup.DataBind()
        Dim cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("myprovalConnectionString").ConnectionString)
        Try
            Dim cmd As New System.Data.SqlClient.SqlCommand
            cmd.Connection = cnn
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.CommandText = "dbo.ReportGroupSelect"
            cmd.Parameters.Add("@ReportTypeGroupKey", Data.SqlDbType.Bit).Value = True
            cnn.Open()
            Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader
            If dr.HasRows Then
                ReportGroup.DataTextField = "ReportTypeGroupName"
                ReportGroup.DataValueField = "ReportTypeGroupKey"
                ReportGroup.DataSource = dr
                ReportGroup.DataBind()
            End If
        Finally
            cnn.Close()
        End Try
    End Sub

Open in new window

0
 

Author Comment

by:rcowen00
ID: 36568815
By the way, I am getting an error when I call the Stored Procedure

'ReportGroup' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36569249
The stored procedure shown in the VB code is not the same as the one you have in the question. Further if it is meant to be, note that on the SQL side, the parameter is INT whereas in .NET you made it BIT. Additionally, try Parameters.AddWithValue as an alternative, but think it is the data type issue.
0
 

Author Comment

by:rcowen00
ID: 36569449
Thank you, I will give that a try.
0
 

Author Comment

by:rcowen00
ID: 36569874
No, it is not the original stored procedure referenced, but I am having the same problem with both.  Why would the Stored Procedure?  I am including the Where Clause
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36570012
For sake of troubleshooting stick with one of the stored procedures at a time to limit variables. To that point was my question on value of parameter you are passing. Test the same value in SSMS to make sure there are actually rows that meet that criteria before wasting your time.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:rcowen00
ID: 36570329
So I believe the problem is that the web form I am building needs to be able to accept a null value (no selection made yet).   Does that make sense?
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 36573156
PAramet name is different in code and procedure.
0
 

Author Comment

by:rcowen00
ID: 36575159
Thank you Patel, but mwvisa1 had already noted that.  I am having the same problem in 2 locations with Stored Procedure and I uploaded the incorrect one.  I have attached it in case.  Thank you.
Private Sub LoadReport()
        Dim cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("myprovalConnectionString").ConnectionString)
        Try
            Dim cmd As New System.Data.SqlClient.SqlCommand
            cmd.Connection = cnn
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.CommandText = "dbo.ReportTypeSelect"
            cmd.Parameters.Add("@ReportTypeKey", Data.SqlDbType.Int).Value = Session("RTKey").ToString
            cnn.Open()
            Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader
            If dr.HasRows Then
                dr.Read()
                ReportName.Text = dr("ReportTypeName")
                ShortDesc.Text = dr("ReportTypeDesc")
                ReportGroup.SelectedValue = (dr("ReportTypeGroupKey"))
                OrderType.SelectedValue = (dr("OrderTypeKey"))
                
            End If
            dr.Close()
        Finally
            cnn.Close()
        End Try
    End Sub

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36575658
Here, you can try this:
cmd.Parameters.Add("@ReportTypeKey", Data.SqlDbType.Int)
cmd.Parameters("@ReportTypeKey").Value = {integer value here}

I would recommend creating an integer variable and set it to a proper default. Check the session variable RTKey is actually numeric before setting the value of variable, e.g., TryParse.

Additional information below.

Here is a reference for Parameters.AddWithValue().
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx

Configuring Parameters and Parameter Data Types (ADO.NET)
http://msdn.microsoft.com/en-us/library/yy6y35y8(v=VS.100).aspx
(more formally written out method)
0
 

Author Comment

by:rcowen00
ID: 36576363
mwvisa1,

Ok, so the form now opens without the error but, the fields are empty.  The RTKey is an int (7).  Any suggestions?  Thank you so much for your time.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36576383
What is the new code and do you have an RTKey in Session that should yield results? i.e., what is RTKey currently?
0
 

Author Comment

by:rcowen00
ID: 36576411
Dependent on what the user is doing (editing existing record or adding a new record) I may or may not have a RTKey.  I would like if the RTKey is available to populate the form with the existing data but allow for edits or if the RTKey is not available return a blank form with the ability to add.
Private Sub LoadReport()
        Dim cnn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("myprovalConnectionString").ConnectionString)
        Try
            Dim cmd As New System.Data.SqlClient.SqlCommand
            cmd.Connection = cnn
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.CommandText = "dbo.ReportTypeSelect"
            cmd.Parameters.Add("@ReportTypeKey", Data.SqlDbType.Int)
            cmd.Parameters("@ReportTypeKey").Value = 0
            cnn.Open()
            Dim dr As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader
            If dr.HasRows Then
                dr.Read()
                ReportName.Text = dr("ReportTypeName")
                ShortDesc.Text = dr("ReportTypeDesc")
                ReportGroup.SelectedValue = (dr("ReportTypeGroupKey"))
                OrderType.SelectedValue = (dr("OrderTypeKey"))
                
            End If
            dr.Close()
        Finally
            cnn.Close()
        End Try
    End Sub

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36576596
So you basically need to add an IF statement around the SqlConnection/SqlCommand code. Check if IsNothing(Session("RTKey")) is true. If so, then do nothing here unless you do not have form with field to enter an RTKey elsewhere. If RTKey is not nothing and is numeric, then pass it as an integer to your sql command.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dynamics crm 2011 8 46
PL/SQL query 14 51
Managing SQL log files, SQL Server 2014 6 56
Updating variable table 9 17
In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now