Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Stored Procedure Results

Posted on 2011-09-20
15
228 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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
 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

792 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