I'm beginner in C#. I have some buttons which are disabled by default in the main form. After the user signs in I want them to be enabled and available for the user to work with. It's kind of an access modifier.
The buttons are in the main form and the login form is within another form. After user pressed a login button (checking the username and password) i want to enable those button in the main form.below is my code
private void btnLogic_Click(object sender, EventArgs e)
{
FormSettingLogic f2 = new FormSettingLogic();
f2.Owner = this;
f2.Show();
}
private void btnAddFile_Click(object sender, EventArgs e)
{
FormLogin fl = new FormLogin();
fl.Owner = this;
....
....
}
private void btnLoadManteg_Click(object sender, EventArgs e)
{
Form_LoadMantegh f6 = new Form_LoadMantegh();
f6.Owner = this;
f6.Show();
}
and user's login code
public FormLogin()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String abc;
try
{
sqlConnection1.Open();
SqlCommand cmd = new SqlCommand("select * from [Users] where userName=@uName and password = @Pass ", sqlConnection1);
SqlDataAdapter sda;
sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.SelectCommand.Parameters.AddWithValue("@uName", txtUname.Text);
sda.SelectCommand.Parameters.AddWithValue("@Pass", txtpass.Text);
abc =Convert.ToString (cmd.ExecuteScalar()); //execute the query and get the user Id and put it in abc variable
DataTable dt = new DataTable();
sda.Fill(dt);
sqlConnection1.Close();
if (dt.Rows.Count == 0)
{
MessageBox.Show("Username or Password is incorrect!");
}
else
{
this.Hide();
//MessageBox.Show(abc);
sendID.send = abc;
(this.Owner as Form_Main).btnAddFile.Enabled = true;
(this.Owner as Form_Main).btnLogic.Enabled = true;
}
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
Form_Main_load code :
private void Form_Main_Load(object sender, EventArgs e)
{
btnLogic.Enabled = false;
btnAddFile.Enabled = false;
btnLoadManteg.Enabled = false;
}
and the Error is: "Object reference not set to an instance of an object"