-1

Possible Duplicate:
passing data between two forms using properties

I am using the following code to validate a login on a winform application.

public int Validate_Login(String Username, String Password)
        {

            //SqlConnection con = new SqlConnection(@"User id=judgment_day;Password=Kit$hen;Server=216.40.232.82, 2153;Database=new_judgment-system");
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["rawr"].ConnectionString);
            SqlCommand cmdselect = new SqlCommand();
            cmdselect.CommandType = CommandType.StoredProcedure;
            cmdselect.CommandText = "[dbo].[prcLoginv]";
            cmdselect.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = Username;
            cmdselect.Parameters.Add("@UPassword", SqlDbType.VarChar, 50).Value = Password;
            cmdselect.Parameters.Add("@OutRes", SqlDbType.Int, 4);
            cmdselect.Parameters["@OutRes"].Direction = ParameterDirection.Output;
            cmdselect.Connection = con;
            int Results = 0;

            try
            {
                con.Open();
                cmdselect.ExecuteNonQuery();
                Results = (int)cmdselect.Parameters["@OutRes"].Value;
            }
            catch (SqlException ex)
            {
                lblMessage.Text = ex.Message;
            }
            finally
            {
                cmdselect.Dispose();

                if (con != null)
                {
                    con.Close();
                }
            }
            return Results;
        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            int Results = 0;


            if (txtUsername.Text != null && txtPassword.Text != null)
            {



                Results = Validate_Login(txtUsername.Text, txtPassword.Text);



                if (Results == 1)
                {



                    lblMessage.Text = "Login is Good";

                    this.Hide();
                    frmSwitch frm = new frmSwitch();
                    frm.Show();   


                }



                else
                {



                    lblMessage.Text = "Invalid Login";

                    lblMessage.ForeColor = System.Drawing.Color.Red;



                }



            }



            else
            {



                lblMessage.Text = "Please make sure that the username and the password is Correct";



            }
        }

The SP is as follows:

Create Proc [dbo].[prcLoginv]  
 (  
 @Username VarChar(50),   
 @UPassword varChar(50),  
 @OutRes int OUTPUT  
 )  
 AS  
set @OutRes = (SELECT count(*) FROM [dbo].Log_Users   
WHERE Username = @Username And [Password] = @UPassword)  
if(@OutRes = 1)  

begin  
set @OutRes = 1--Login is Correct  
end  
else  
begin  
set @OutRes = 0  --Bad login  
end 

How would I modify that to also return the userID so I can pass it to another form. I need to use that ID to show who is working on what file.

Community
  • 1
  • 1
korrowan
  • 563
  • 2
  • 15
  • 37
  • Do you need to modify a stored procedure to return the userId or are you questioning how to pass data between forms? If returning the userId, then you don't need to pass back 1 or 0, just pass the userId or something like -1 otherwise. – Alex M Nov 20 '12 at 22:37

1 Answers1

1

Instead of

set @OutRes = (SELECT count(*) FROM [dbo].Log_Users   
WHERE Username = @Username And [Password] = @UPassword)

you could try

select @OutRes = userid from x where blah.

If the query returns no rows you get 0, if there are multiple rows it gives you the last one.

Mike Walsh
  • 198
  • 6
  • Never though of this and this is exactly what I did. Thanks Mike! I always over think this stuff =/. – korrowan Nov 20 '12 at 22:58