using a stored procedure for login in c#

Posted by Jin Yim on Stack Overflow See other posts from Stack Overflow or by Jin Yim
Published on 2010-05-31T03:38:52Z Indexed on 2010/05/31 3:42 UTC
Read the original article Hit count: 235

Filed under:
|
|
|
|

Hi all,

If I run a store procedure with two parameter values (admin, admin) (parameters : admin, admin) I get the following message :

Session_UID User_Group_Name Sys_User_Name


NULLAdministratorsNTMSAdmin No rows affected. (1 row(s) returned) @RETURN_VALUE = 0 Finished running [dbo].[p_SYS_Login].

-- To get the same message in c# I used the code following :

        string strConnection = Settings.Default.ConnectionString;           
        using (SqlConnection conn = new SqlConnection(strConnection))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                SqlDataReader rdr = null;
                cmd.Connection = conn;
                cmd.CommandText = "p_SYS_Login";
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter paramReturnValue = new SqlParameter();
                paramReturnValue.ParameterName = "@RETURN_VALUE";
                paramReturnValue.SqlDbType = SqlDbType.Int;
                paramReturnValue.SourceColumn = null;
                paramReturnValue.Direction = ParameterDirection.ReturnValue;

                cmd.Parameters.Add(paramReturnValue);

                cmd.Parameters.Add(paramGroupName);
                cmd.Parameters.Add(paramUserName);
                cmd.Parameters.AddWithValue("@Sys_Login", "admin");
                cmd.Parameters.AddWithValue("@Sys_Password", "admin");

                try
                {
                    conn.Open();
                    rdr = cmd.ExecuteReader();
                    string test = (string)cmd.Parameters["@RETURN_VALUE"].Value;
                    while (rdr.Read())
                    {
                       Console.WriteLine("test : " + rdr[0]);
                    }
                }
                catch (Exception ex)
                {
                    string message = ex.Message;
                    string caption = "MAVIS Exception";
                    MessageBoxButtons buttons = MessageBoxButtons.OK;

                    MessageBox.Show(
                    message,
                    caption,
                    buttons,
                    MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1);
                }
                finally
                {
                    cmd.Dispose();
                    conn.Close();
                }
            }
        }

but I get nothing in SqlDataReader rdr ; is there something I am missing ?

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET