Solved

Stored Procedure Results

Posted on 2011-09-20
15
226 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

911 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

18 Experts available now in Live!

Get 1:1 Help Now