0

I use winforms c#

I am trying to get userid from userinfo table and then inserted that userid in another table for this first i try this

updated code

ok i try this

public class Login
{
string email;
string password;
public int userid;

public Login(string UserEmail, string UserPassword)
{
    email = UserEmail;
    password = UserPassword;
}

public string Logined()
{
    con.Open();
    cmd = new SqlCommand("SELECT userid from UserInformation WHERE UserEmail='" + email + "'and UserPassword='" + password + "'", con);
    cmd.Parameters.AddWithValue("@UserEmail", email);
    cmd.Parameters.AddWithValue("@UserPassword", password);
    cmd.ExecuteNonQuery();
    SqlDataReader dr = cmd.ExecuteReader();

    if (dr.HasRows)
    {
        Categorys obj = new Categorys();
        obj.ShowDialog();
        while (dr.Read())
        {
            userid = Convert.ToInt32(dr["userid"]);
        }
        return msg = "You Are Successfully Login!";
    }
    else
    {
        return msg = "Sorry Incorrect Password Or Email!";
    }
}

And this for your button_click else if

 else if (comboBox1.Text == "Admin.")
            {
                this.Hide();
                string UserEmail = textBox2.Text;
                string UserPassword = textBox1.Text;
                Login objlogin = new Login(UserEmail, UserPassword);
                textBox3.Text = Convert.ToString(objlogin.userid);
                MessageBox.Show(objlogin.Logined());
                savedid = objlogin.userid;
                Categorys obj = new Categorys();
                obj.ShowDialog();
                con.Close();
            }

and in new form i try this on form load for call id

private void Categorys_Load(object sender, EventArgs e)
    {
        Login_Form l = new Login_Form();
        textBox2.Text = Convert.ToString(l.savedid);
    }

but this shows 0 in textbox

user 123
  • 41
  • 2
  • 13
  • Regardless of anything else, please read up on [SQL injection](http://stackoverflow.com/search?q=sql+injection+%5B.net%5D) and how to prevent it and [how _not_ to store passwords in a database](http://stackoverflow.com/search?q=store+password+in+database+%5B.net%5D). – Christian.K Apr 15 '17 at 15:17

1 Answers1

1

If it's a new Form you could use the constructor of that class to pass the parameters you want.
Something like that should work: (I used a label too show what has been passed)

public partial class Form2 : Form
{
    string rec;
    public Form2(string rec)
    {
        InitializeComponent();
        this.rec = rec;
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = rec;
    }
}

And then calling it like:

Form secondForm = new Form2("Hello there");
secondForm.ShowDialog();

For your updated version Try this for your login class

public class Login
{
    string email;
    string password;
    public int userid;

    public Login(string UserEmail, string UserPassword)
    {
        email = UserEmail;
        password = UserPassword;
    }

    public string Logined()
    {
        con.Open();
        cmd = new SqlCommand("SELECT userid from UserInformation WHERE UserEmail='" + email + "'and UserPassword='" + password + "'", con);
        cmd.Parameters.AddWithValue("@UserEmail", email);
        cmd.Parameters.AddWithValue("@UserPassword", password);
        cmd.ExecuteNonQuery();
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.HasRows)
        {
            Categorys obj = new Categorys();
            obj.ShowDialog();
            while (dr.Read())
            {
                userid = Convert.ToInt32(dr["userid"]);
            }
            return msg = "You Are Successfully Login!";
        }
        else
        {
            return msg = "Sorry Incorrect Password Or Email!";
        }
    }

And this for your button_click else if... part:

 else if (comboBox1.Text == "Admin.")
        {
            this.Hide();
            string UserEmail = textBox2.Text;
            string UserPassword = textBox1.Text;
            Login objlogin = new Login(UserEmail, 
            MessageBox.Show(objlogin.Logined());
            textBox3.Text = Convert.ToString(objlogin.userid);
            Categorys obj = new Categorys(objlogin.userid);
            obj.ShowDialog();
            con.Close();
        }

Just change the Categorys constructor to take an integer, assign it to a global variable in the Categorys class and then you can use it in the onload Method like textbox1.Text = Convert.toString(categorys_instance_of_userid)

Pio
  • 513
  • 4
  • 19
  • regarding that I found [this](http://stackoverflow.com/questions/4018114/read-data-from-sqldatareader) question. May it help you – Pio Apr 15 '17 at 14:50
  • ok now i add this in logined function .. while(dr.Read()) { int userid = Convert.ToInt32(dr["userid"]); } but how i use this userid in button click ? – user 123 Apr 15 '17 at 15:00
  • CHECK UPDATED CODE PLEASE – user 123 Apr 15 '17 at 15:08
  • you could assign the userid to a global variable in your login class and access is it in the button_click function after you call `MessageBox.Show(objlogin.Logined());` – Pio Apr 15 '17 at 15:09
  • when i do this .. Login objlogin = new Login(UserEmail, UserPassword, loginid); textBox3.Text = Convert.ToString(loginid); this shows unassigned error – user 123 Apr 15 '17 at 15:13
  • thanku if i want to call only userid in anther form then? i do this but with this useremail and user password also needs to be called and i only want to display userid – user 123 Apr 15 '17 at 15:45
  • just pass it onto the form the way you did in your login form. `Form form3 = new Form3(userid,usermail,password) ...` – Pio Apr 15 '17 at 16:00
  • please check updated code when i try to call this shows 0 id in another form .. and yes your answer is helpful – user 123 Apr 15 '17 at 16:38
  • yes but i dont want to pass useremail and password in new form i only want to pass uuserid .. if i am trying to pass then this shows error that useremail and password have to pass – user 123 Apr 15 '17 at 16:44
  • can you show where you create the Categorys form? Do I get this right: the your first form is the one with the button click, it calls the login form and then you want to go further to the categorys form? – Pio Apr 15 '17 at 16:48
  • i have login form in that there is user email and password text and from data i get userid successfully in login form but when i try to call that id in any other form (i,e categroy form) then this shows 0.. where as in category form there is no email and password text box – user 123 Apr 15 '17 at 16:53
  • i create separate form – user 123 Apr 15 '17 at 16:55
  • okay you have to use the same Login object to pass the id. You should practice some easier examples with oo-programming first I think. The userid is currently in `objlogin.userid` so if you want to pass it to your new form you would have to do something like that: `Form categroy_form = new Categorys(objlogin.userid)` sometime after you called the `Logined()`method and then on the Category_onLoad method ` textBox2.Text = Convert.ToString(*whatever the variable is called in this class*); ` – Pio Apr 15 '17 at 17:07
  • right now you're creating an new instance of Login in the `Categorys_Load(...)` method and this instance never performed the sql select so it **does not** know about the userid you got in your `objlogin.Logined()` call – Pio Apr 15 '17 at 17:14
  • this Form categroy_form = new Categorys(objlogin.userid) where i call on categorm form load or somewhere else? – user 123 Apr 15 '17 at 17:16
  • ok i create categroy constructor but this shows error public Categorys() { InitializeComponent(); Form categroy_form = new Categorys(objlogin.userid); Error 1 The name 'objlogin' does not exist in the current context } – user 123 Apr 15 '17 at 17:31
  • no, you just write in there `public Categorys(int userid) { InitializeComponent(); this.userid = userid; } `if you add a global variable userid to the categorys class and use my updated `else if...` part it should work – Pio Apr 15 '17 at 17:36