Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Stored Procedure Results

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
rcowen00
Asked:
rcowen00
  • 8
  • 6
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
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
 
rcowen00Author Commented:
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
 
rcowen00Author Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Kevin CrossChief Technology OfficerCommented:
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
 
rcowen00Author Commented:
Thank you, I will give that a try.
0
 
rcowen00Author Commented:
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
 
Kevin CrossChief Technology OfficerCommented:
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
 
rcowen00Author Commented:
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
 
Alpesh PatelAssistant ConsultantCommented:
PAramet name is different in code and procedure.
0
 
rcowen00Author Commented:
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
 
Kevin CrossChief Technology OfficerCommented:
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
 
rcowen00Author Commented:
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
 
Kevin CrossChief Technology OfficerCommented:
What is the new code and do you have an RTKey in Session that should yield results? i.e., what is RTKey currently?
0
 
rcowen00Author Commented:
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
 
Kevin CrossChief Technology OfficerCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now