Reputation: 41
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
Upvotes: 0
Views: 1379
Reputation: 533
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)
Upvotes: 1