Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am working on asp.net 2010 witgh sql express edition 2008.

I am trying to retrieve the following :-

I have a credentiuals table....i have a stored proc in whoich i am passing two inputs and expecting three outputs.

INputs: uid and password
output: booeanyesORno , Uname, Utype

Also, On the click of a button ,I wrote the following code:
....make a conection...
....setting a stored proc instead of a select query...very easy..
then...
cmd.parameters.addwithvalue("@uid", textbox1.text);
cmd.parameters.addwithvalue("@pwd", textbox2.text)

cmd.parameters.add("@booeanyesORno",sqldbtype.bit).direction=parameterdirection.output
cmd.parameters.add("@uname";,sqldbtype.varchar).direction=parameterdirection.output
cmd.parameters.add("@utype",sqldbtype.varchar).direction=parameterdirection.output


bool bb=(bool)cmd.executescalar();

=================================================


create proc pp(@uid varchar(20),@pwd varchar(20),@booeanyesORno bit output, @uname varchar(20) output, @utype varchar(20) output
as
begin
select @booeanyesORno=count(*) from tablename where userid=@uid and password =@pwd 

select @uname=username,usertype= @utype from tablename where userid=@uid and password =@pwd
end

---------------------------------------------
NOW, THIS SP is working all fine...bnut the issue is on the coding sode....my prob is that i want to retrieve the three outputs in the code behind(so that on home page the cmr could see the uname and utype)...
-i cant use nonquery and datareader but its not worth..i tried scalar method...but i dont know how to go ahead and retrieve the values whic hare succesfully in the coe-behind...Please help !!!!


Any other inputs(if you need) ..please ask...
- here, pls dont go by case-senstiveness

pLEASE HELP SOON...job in danger...

[edit]Code blocks added - OriginalGriff[/edit]
Posted
Updated 18-Jan-11 1:04am
v2

Here is an example showing how I would do it - input and output:
public static bool PostalAreaInsert( SqlConnection sqlConnection, ref Guid oid, ref DateTime? registeredTime, ref Guid? registeredBy, Guid? region, string name, DateTime? fromTime, DateTime? throughTime)
        {
            bool result = false;
            SqlCommand sqlCommand = sqlConnection.CreateCommand( );
            using( sqlCommand )
            {
                sqlCommand.CommandText = "PostalAreaInsert";
                sqlCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter oidParameter = 
                   sqlCommand.Parameters.Add(new SqlParameter("@Oid",
                         SqlDbType.UniqueIdentifier));
                oidParameter.Direction = ParameterDirection.InputOutput;
                oidParameter.Value = oid;
                SqlParameter registeredTimeParameter =
                     sqlCommand.Parameters.Add(new SqlParameter
                        ("@RegisteredTime", SqlDbType.DateTime));
                registeredTimeParameter.Direction =
                      ParameterDirection.InputOutput;
                if( registeredTime.HasValue )
                {
                    registeredTimeParameter.Value = 
                        registeredTime.Value;
                }
                else
                {
                    registeredTimeParameter.Value = DBNull.Value;
                }
                SqlParameter registeredByParameter = 
                          sqlCommand.Parameters.Add(
                            new SqlParameter("@RegisteredBy", 
                              SqlDbType.UniqueIdentifier));
                registeredByParameter.Direction = 
                          ParameterDirection.InputOutput;
                if( ( registeredBy.HasValue ) 
                   && ( registeredBy.Value != Guid.Empty ) )
                {
                    registeredByParameter.Value = registeredBy.Value;
                }
                else
                {
                    registeredByParameter.Value = DBNull.Value;
                }
                SqlParameter objectTypeParameter = 
                     sqlCommand.Parameters.Add(
                       new SqlParameter("@ObjectType", SqlDbType.Int));
                objectTypeParameter.Direction = 
                     ParameterDirection.InputOutput;
                objectTypeParameter.Value = 2;
                SqlParameter regionParameter = 
                      sqlCommand.Parameters.Add(
                         new SqlParameter("@Region", 
                            SqlDbType.UniqueIdentifier));
                if( ( region.HasValue ) 
                    && ( region.Value != Guid.Empty ) )
                {
                    regionParameter.Value = region.Value;
                }
                else
                {
                    regionParameter.Value = DBNull.Value;
                }
                SqlParameter nameParameter = 
                    sqlCommand.Parameters.Add(
                      new SqlParameter("@Name", 
                        SqlDbType.NVarChar, 255));
                if(( name != null )
                   && ( name.Length > 0 ))
                {
                    nameParameter.Value = name;
                }
                else
                {
                    nameParameter.Value = DBNull.Value;
                }
                SqlParameter fromTimeParameter = 
                    sqlCommand.Parameters.Add(
                      new SqlParameter("@FromTime", 
                        SqlDbType.DateTime));
                if( fromTime.HasValue )
                {
                    fromTimeParameter.Value = fromTime.Value;
                }
                else
                {
                    fromTimeParameter.Value = DBNull.Value;
                }
                SqlParameter throughTimeParameter = 
                     sqlCommand.Parameters.Add(
                         new SqlParameter("@ThroughTime", 
                                    SqlDbType.DateTime));
                if( throughTime.HasValue )
                {
                    throughTimeParameter.Value = throughTime.Value;
                }
                else
                {
                    throughTimeParameter.Value = DBNull.Value;
                }
                int rowsAffected = sqlCommand.ExecuteNonQuery();
                if(rowsAffected == 1)
                {
                    Guid newOid = (Guid)oidParameter.Value;
                    DateTime regTime = 
                         Convert.ToDateTime
                           (registeredTimeParameter.Value);
                    Guid regByOid = (Guid)registeredByParameter.Value;
                    oid = newOid;
                    registeredTime = regTime;
                    registeredBy = regByOid;
                    result = true;
                }
            }
            return result;
        }


The stored procedure:
CREATE PROCEDURE [PostalAreaInsert]
  @Oid uniqueidentifier OUTPUT,
  @RegisteredTime DateTime OUTPUT,
  @RegisteredBy uniqueidentifier OUTPUT,
  @ObjectType int OUTPUT,
  @Region uniqueidentifier,
  @Name nvarchar(255),
  @FromTime DateTime,
  @ThroughTime DateTime
AS
  BEGIN
    IF @Oid IS NULL
    BEGIN
      SET @Oid = NEWID()
    END
    IF @ObjectType IS NULL
    BEGIN
      SET @ObjectType = 2
    END
    IF @RegisteredBy IS NULL
    BEGIN
      SET @RegisteredBy = (SELECT Oid From SecurityLogin WHERE [Identity] = SYSTEM_USER)
    END
    IF @RegisteredTime IS NULL
    BEGIN
      SET @RegisteredTime = SYSDATETIME()
    END
    DECLARE @TranCounter INT;
    SET @TranCounter = @@TRANCOUNT;
    IF @TranCounter > 0
      SAVE TRANSACTION SavePoint2;
    ELSE
      BEGIN TRANSACTION;
    BEGIN TRY
      INSERT INTO Element(Oid,OptimisticLockField,RegisteredBy,RegisteredTime,ObjectType)
          VALUES(@Oid,0,@RegisteredBy,@RegisteredTime,@ObjectType);
      INSERT INTO [PostalArea](Oid, [Region], [Name], [FromTime], [ThroughTime])
          VALUES(@Oid, @Region, @Name, @FromTime, @ThroughTime);
      IF @TranCounter = 0
          COMMIT TRANSACTION;
    END TRY
    BEGIN CATCH
        DECLARE @ErrorMessage NVARCHAR(4000);
        DECLARE @ErrorSeverity INT;
        DECLARE @ErrorState INT;
        SELECT @ErrorMessage = ERROR_MESSAGE(),
            @ErrorSeverity = ERROR_SEVERITY(),
            @ErrorState = ERROR_STATE();
        IF @TranCounter = 0
          ROLLBACK TRANSACTION;
        ELSE
          IF XACT_STATE() <> -1
            ROLLBACK TRANSACTION SavePoint2;
        RAISERROR(
            @ErrorMessage,
            @ErrorSeverity,
            @ErrorState);
    END CATCH
  END
GO


Regards
Espen Harlinn
 
Share this answer
 
v2
Why not have a look at the 'Using OUTPUT variables' part of this article

http://www.sqlteam.com/article/stored-procedures-returning-data[^]

e.g.

C#
// Your code should get the connection string from web.config
string connectionString =
  @"Server=L40\YUKON; Initial Catalog=AdventureWorks; Integrated Security=True;";
using (SqlConnection conn = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand("dbo.GetCountByLastName"))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@LastName", "Alexander"));
        SqlParameter countParameter = new SqlParameter("@LastNameCount", 0);
        countParameter.Direction = ParameterDirection.Output;
        cmd.Parameters.Add(countParameter);
        conn.Open();
        cmd.Connection = conn;
        cmd.ExecuteNonQuery();
        int count = Int32.Parse(cmd.Parameters["@LastNameCount"].Value.ToString());
        Response.Write("<p>Count: " + count.ToString());
        conn.Close();
    }
}


After a call to ExecuteNonquery, just retrieve each of the parameters you need

A simple google for 'get output parameters from stored procedure' and this was the top hit. If you want to keep your job safe, I think learning how to google should be your number 1 goal
 
Share this answer
 
Comments
shikhar gilhotra 19-Jan-11 6:24am    
hey ,this code is not accessible
regards..
u can create a like this

Your query is wrong in case of u get a multiplier data from one store producer

you cant not execute the scaler commend cos u get data not true or false
:omg: :omg: :omg: :omg:
 
Share this answer
 
if u want the three items from data base u can simply do like this



cmd = new SqlCommand("USP_GetMaxJobNo", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@uniqId", uniqid);
        cmd.Parameters.AddWithValue("@username", username);
        DataSet ds = new DataSet();
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        ConClose();
        return ds;




u can use dataset or datatable
 
Share this answer
 
v2
Comments
shikhar gilhotra 18-Jan-11 7:11am    
hey ..i dont want to use the disconnectwed mode...
IN THE CASE OF SQL QUERY

YOUR QUERY RETURN ONE AND MORE DATA SO YOUR DATA CANT HANDLE THIS

SQL
select @booeanyesORno=count(*) from tablename where userid=@uid and password =@pwd
select @uname=username,usertype= @utype from tablename where userid=@uid and password =@pwd


CHANGE THIS QUERY AND GET ONLY ONE DATA
BY USING UNION
:rolleyes: :rolleyes: :rolleyes:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900