Question

EmployeeTrackDatacontextDataContext db = new EmployeeTrackDatacontextDataContext();
DataSet logStatusDs = new DataSet();

I have a Dataset as :

public DataSet Sequence(Int32 userId)
{
    //load the List one time to be used thru out the intire application
    var ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["EMSJConnectionString"].ConnectionString;
    using (SqlConnection connStr = new SqlConnection(ConnString))
    {
        using (SqlCommand cmd = new SqlCommand("get_LoginStatusQ", connStr))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserId", userId);
            cmd.Connection.Open();
            new SqlDataAdapter(cmd).Fill(logStatusDs);

            //DataTable dt = logStatusDs.Tables[0].Rows[0];
        }
    }

    return logStatusDs;
}

Stored procedure I am using is :

ALTER PROCEDURE [dbo].[get_LoginStatusQ]  
   @UserId int
AS   
BEGIN  
   SET NOCOUNT ON;

   SELECT TOP 1 LoginStatus 
   FROM UserTime 
   WHERE UserId = 2 
   ORDER BY UserTimeId DESC
END

which returns this data:

         LoginStatus
       ________________   
       1 |    In     |

Method

Sequence(Convert.ToInt32(Session["userid"])) 

I run it on PageLoad

After this method I want to get the return value from stored procedure which is "In" and compare it in IF statement. how can I do this?

Ex:

If(ReturnValueOfStoredProcedure != "In")
{   do stuf...}
else
{  do stuff...  }      

Examples would be appreciated.

Was it helpful?

Solution

You are just returning a single value from a query. This could be done using ExecuteScalar instead of building and filling a DataSet and its table

public string  Sequence(Int32 userId)
{
    //load the List one time to be used thru out the intire application
    var ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["EMSJConnectionString"].ConnectionString;
    using (SqlConnection connStr = new SqlConnection(ConnString))
    {
        using (SqlCommand cmd = new SqlCommand("get_LoginStatusQ", connStr))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@UserId", userId);
            cmd.Connection.Open();
            object result = cmd.ExecuteScalar();
            return result == null ? string.Empty : result.ToString()
        }
    }
}

now the call is simply

string status = Sequence(Convert.ToInt32(Session["userid"])) 
if(status == "In")
{  

}
else
{

}

The only thing to look for is the return value from the ExecuteScalar that could be null if the query doesn't find the user with the given ID

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top