Latest developer linksBookmark and Share
 
HomeThis WeekTop MonthTop AlltimeSearchRegisterFAQ
  
 
Submit Your Link
Please login to submit your Link
 

Returning query AND output parameter

Posted: Jan/22/2011   By: nikhil   Points:15   Category: .NET  - ASP.Net    Views:121   Vote Up (0)   Vote Down (0)    

Is it possible to return a OUTPUT parameter AND a query from a stored proecedure?  I seem to be able to do one or the other, but doing both isn't.  Here is my sample...

Stored Procedure

ALTER PROCEDURE dbo.GetUsersAfter

(

 @after DateTime,

 @time DateTime OUTPUT

) 

AS

BEGIN

 SET @time = GetUTCDate();

 SELECT 

  ID,

  LastName, 

  FirstName

  FROM Users

  WHERE Stored > @after;

END

C# Code to Access

public List<User> GetUsersAfter(ref DateTime after)

{

 using (SqlConnection con = MSSQLConnection)

 {

  using (SqlCommand cmd = new SqlCommand("GetUsersAfter", con))

  {

   cmd.CommandType = CommandType.StoredProcedure;

   cmd.Parameters.Add("@after", SqlDbType.DateTime, 0).Value = after;

   SqlParameter rtn = cmd.Parameters.Add("@time", SqlDbType.DateTime, 0);

   rtn.Direction = ParameterDirection.Output;

   try

   {

    con.Open();

    using (SqlDataReader dr = cmd.ExecuteReader())

    {

     List<User> lst = ReadPositions(dr);

     after = (DateTime)rtn.Value;</strong>  <strong>// <=== FAILS HERE

     return lst;

    }

   }

   finally

   {

    con.Close();

   }

  }

 }

}

Stepping through the code, it fails on the assignment of the output parameter.  rtn.Value is equal to null.

Doing a search on the web or in this forum (so far...) have examples of one or the other. 

Thanks for your help.

 

View Complete Post


Comments:
Be the first to comment this post.
 
Post Comment
Please login to post your comment
More Related Resources

Microsoft JScript runtime error: Sys.ArgumentException: Value must not be null for Controls and Behaviors. Parameter name: element

  

Hello,

I am facing this error 

Microsoft JScript runtime error: Sys.ArgumentException: Value must not be null for Controls and Behaviors.
Parameter name: element

Microsoft JScript runtime error: Sys.ArgumentException: Value must not be null for Controls and Behaviors.

Parameter name: element

while using Ajaxtoolkit ModelPopupextender with listview, actually I am retriving image from database to the listview in VS 2008. 

Can anyone please help me out of this..


Webmatrix.Data.Database.Query() method return type

  

I'm using the Webmatrix.Data.Database.Query method to return a result set in the following form

var myResult = db.Query("Select * from mytable");

How do you cast myResult to get the count of records returned? I'd like to do something like

   myResult.Count


Query String Problem

  

Hi,

I am trying to grab data from a query string but I am having problems as the id within the query string contains a '#' symbol so for example

payment.aspx?id=#12

Now the problem is when I use  string id=Request["id"] it doesn't grab the id but does if I remove the hash, any ideas anybody?

Thanks

Steven


SQL QUERY PARAMETER INPUT

  

Using a text box to accept user input I would like accept the value from the textbox for @DESCRIPTION

I'm getting stuck on that part. How do I get the input from the text box into my SELECT parameter so it will search on this value ?

Code example below;

THANKS FOR YOU HELP !!

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

 
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        
        Dim SQLConnStr As String = ConfigurationManager.ConnectionStrings("IMDEquipDBConnectionString").ConnectionString


        Dim selectSQL As String = "SELECT * FROM tbl3161IMD WHERE DESCRIPTION LIKE @DESCRIPTION"


        Dim mySQLconn As New SqlConnection(SQLConnStr)

        mySQLconn.Open()

        Dim SQLCommand As New SqlCommand(selectSQL, mySQLconn)

        

        Dim adapter As New SqlDataAdapter(SQLCommand)

        Dim ds As New DataSet

        adapter.Fill(ds, "tbl3161IMD")



        GridView1.DataSource = ds
        GridView1.DataBind()

        mySQLconn.Close()




    End Sub
End Class


 


A parameter with a wildcard in CONTAINS

  

Hello,

I'm trying to create a query to my database with full-text search. I need to pass it a parameter and append it with a '*' wildcard. E.g.:

Dim sqlQuery As String = "SELECT * FROM Table WHERE CONTAINS(content, @parameter)"
sqlCommand.Parameters.AddWithValue("@parameter", someStringValue + "*")



Now what I mean is that I need queries like these:

 

select * from Table where contains(content,'"Two words"')
select * from Table where contains(content,'"word*"')


The first query would return all rows, where the content column contains "Two words" in the exact form and the second query would return all rows, where the content column contains words with "word" + its suffixes.

 Thank you for your help!!

 

 

 

passing parameters for a SELECT query to another page

  

I have an asp.net site which includes a "search" page that queries an SQL table, then redirects the user to a "results" page where the results of the query are displayed.

The parameters for the query are assembled into a string which is the SELECT statement.

I've been asked to add additional functionality that requires a secondary search which takes place on the "results" page. How can I pass the parameters from the "search" page to the "results" page so that I can access those parameters in the code behind? 

Thanks for any help.

Here's the first page code behind (I inherited most of this from another developer):

using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Maine_Innkeeper_Site_2010
{
    public partial class searchtest : System.Web.UI.Page
    {
        private string name = string.Empty;
        private string city = string.Empty;
        private string desc = string.Empty;
        private string propType = string.Empty;
        private string regionStr = string.Empty;
        private List<string> amenities = new List<string>();
        SqlCommand cmd = new SqlCommand();

        //if the form is submitted...
        protected void submit_Click(object sender, EventArgs e)
        {
            if (gatherDataFromForm())
            {
                string sql = buildSQLStatement();
                Session["lodgingSearchSql"] = sql;
                Response.Redirect("lodging-search-output.aspx");
            }
        }

        //if the image map is clicked...
        protected void ImageMapClicked(object sender, ImageMapEventArgs e)
        {
            //establish a variable for the value of the region clicked  
            string Region = e.PostBackValue; 

            //build the query
            bool needAnd = false;
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT ID, PropertyName, City, Image1, Image2, InnLinkURL, EnhancedListing ");
            sb.Append("FROM members WHERE Region = '" + Region +"' ORDER BY City, PropertyName");
            string sql = sb.ToString();
            Session["lodgingSearchSql"] = sql;
            Response.Redirect("lodging-search-output.aspx"); 
        }

        private bool gatherDataFromForm()
        {
            name = common.scanForSqlAttack(PropertyName.Text);
            city = common.scanForSqlAttack(PropertyCity.Text);
            desc = common.scanForSqlAttack(PropertyDescription.Text);

            try
            {
                propType =
                   common.scanForSqlAttack(PropertyType.SelectedValue).ToUpper();
            }
            catch
            {
                propType = "";
            }
            try
            {
                regionStr = common.scanForSqlAttack(region.SelectedValue);
            }
            catch
            {
                regionStr = "";
            }
            for (int i = 0; i < amenityList.Items.Count; i++)
            {
                if (amenityList.Items[i].Selected)
                    amenities.Add(amenityList.Items[i].Value);
            }
            return true;
        }

        private string buildSQLStatement()
        {
            bool needAnd = false;
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT ID, PropertyName, City, Image1, Image2, InnLinkURL, EnhancedListing ");
            sb.Append("FROM members WHERE ");
            if (name.Trim() != "")
            {
                //if (ExactName.Checked)
                 //   sb.Append("PropertyName = '" + name.ToUpper() + "'");
                //else 
                sb.Append("PropertyName like '%" + name.ToUpper() + "%'");
                needAnd = true;
            }
            else  // not selected by name, use the other selectors
            {
                if (city.Trim() != "")
                {
                    if (needAnd)
                        sb.Append(" AND ");
                    else needAnd = true;
                    if (ExactCity.Checked)
                        sb.Append("city = '" + city.ToUpper() + "'");
                    else sb.Append("city LIKE '%" + city.ToUpper() + "%'");
                }
                if (regionStr.Trim() != "")
                {
                    if (needAnd)
                        sb.Append(" AND ");
                    else needAnd = true;
                    

SQL Query

  

Hi,

 

I'm stuck on figuring out how to write this query - it's probably some something quite basic but I've been looking at it for too long.

 

I want to search a table for two parameters from the same column where one of the parameters equals a value.  How do I go about this?

 

Sorry for such a simple question!!

 

Thanks

 

Kristin

 


ASP.NET LINQ : MONEY OUTPUT PARAMETER

  

Hi experts,

I am using LINQ.

I have stored procedure as:

ALTER PROCEDURE [dbo].[usp_ATI_OA_GetCalculatedValue]
    @OredrID BIGINT = 0,
    @SubTotal MONEY OUTPUT,
    @ShippingTotal MONEY OUTPUT,
    @TaxTotal MONEY OUTPUT,
    @OrderTotal MONEY OUTPUT
AS
BEGIN
    SELECT
            SubTotal,
            ShippingTotal,
            TaxTotal,
            OrderTotal
    FROM
            tb_OrderDetail
    WHERE
            OrderID = @OredrID
END

And my code behind is:

                decimal? dTotal = 1;
                decimal? dSubTotal = 2;
                decimal? dShipping = 3;
                decimal? dTax = 4;

                OrderDataDataContext DBContext = new OrderDataDataContext();

                dtgProductList.DataSource = DBContext.usp_ATI_OA_GetOrderLineProducts(lngOrderID);
                dtgProductList.DataBind();

                DBContext.usp_ATI_OA_GetCalculatedValue(
                                                                                         lngOrderID,
                                                                                         ref dTotal,
                                                                                         ref dShipping,
                                                                                         ref dTax,
                                                                                         ref dSubTotal
                                                                &

how to bind in query in sqldatasource based on listbox multiple selected item

  

Hi..

I need to bind sql IN query in sqldatasource the IN query will bind on dropdownlist and dropdownlist will show databased on Listbox multiple selection how to do this 

I need to method 

1) normal sql query

2) another one is storeprocedure



 
Categories:
.NET
Java
PHP
C/C++/VC++
HTML/XML
SAP
MainFrames
Data Warehousing
Testing
MySQL
SQL Server
Oracle
Javascript/VB Script
Others
Login
 
 
 
 
 Forgot password
 Contact Us   Terms Of use   Share your knowledge