I'm making a program to ease the overview of certain functions. I currently have the program split up into 3 Forms. A login form, using a very simple SQL login, the mainwindow and then a "New Site" window.
I would like to grab the login from Form1 and pass it to Form2.
Firstly because I would like this value to get returned by a label and also to later create a show/hide administrate users button and to restrict editing to one class of users.
I've tried saving it as a public static int, but this doesnt return anything in my new main form.
Here's the first form;
namespace AvOverview{
public partial class Form1 : Form
{
private string value1 = string.Empty;
public string Value1
{
get { return value1; }
set { value1 = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
txt_Password.PasswordChar = '*';
}
//Connection String
string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
//btn_Submit Click event
private void Button_Submit_Click(object sender, EventArgs e)
{
if (txt_UserName.Text == "" || txt_Password.Text == "")
{
MessageBox.Show("Please provide Username and Password");
return;
}
try
{
//Create SqlConnection
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("Select * from tbl_Login where UserName=@username and Password=@password", con);
cmd.Parameters.AddWithValue("@username", txt_UserName.Text);
cmd.Parameters.AddWithValue("@password", txt_Password.Text);
con.Open();
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
cmd.ExecuteNonQuery();
con.Close();
int count = ds.Tables[0].Rows.Count;
//If count is equal to 1, then show frmMain form
if (count == 1)
{
//MessageBox.Show("Login Successful!");
this.Hide();
FrmMain fm = new FrmMain();
fm.Show();
}
else
{
MessageBox.Show("Login Failed!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(0);
}
private void Button1_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
}}
And here's the second form, if you need it;
namespace AvOverview{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
//btn_LogOut Click Event
this.Hide();
Form1 fl = new Form1();
fl.Show();
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(0);
}
private void FrmMain_Load(object sender, EventArgs e)
{
//SQLconnection string
string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
//SQLconnection
SqlConnection con = new SqlConnection(cs);
con.Open();
string strCmd = "select * from AvSites";
SqlCommand cmd = new SqlCommand(strCmd, con);
SqlDataAdapter da = new SqlDataAdapter(strCmd, con);
DataSet ds = new DataSet();
//Fill combobox list with items from the SQL database
da.Fill(ds);
combo1.ValueMember = "id";
combo1.DisplayMember = "siteName";
combo1.DataSource = ds.Tables[0];
combo1.Enabled = true;
this.combo1.SelectedItem = -1;
this.combo1.SelectedText = "--select--";
cmd.ExecuteNonQuery();
con.Close();
}
private void Combo1_SelectedIndexChanged(object sender, EventArgs e)
{
//Populate Textboxes with data from each entry in the SQLdb
DataRowView rv = combo1.SelectedItem as DataRowView;
if (rv != null)
{
txt_siteName.Text = rv[2].ToString();
txt_siteProjnr.Text = rv[1].ToString();
txt_siteManaip.Text = rv[5].ToString();
txt_ManPass.Text = rv[6].ToString();
txt_txtmanAvpass.Text = rv[8].ToString();
txt_manAvusr.Text = rv[7].ToString();
txt_siteClients.Text = rv[4].ToString();
txt_siteLicenses.Text = rv[3].ToString();
txt_siteLicenseExpi.Text = rv[9].ToString();
txt_siteManUsr.Text = rv[12].ToString();
txt_siteID.Text = rv[0].ToString();
txt_siteAvMove.Text = rv[10].ToString();
txt_siteAvTools.Text = rv[11].ToString();
}
}
private void Chkbox_showHide_CheckedChanged(object sender, EventArgs e)
{
//Show/Hide password text in Manager Textbox
if (chkbox_showHide.Checked)
{
txt_ManPass.UseSystemPasswordChar = true;
}
else
{
txt_ManPass.UseSystemPasswordChar = false;
//txt_ManPass.PasswordChar = chkbox_showHide.Checked ? '\0' : '*';
}
}
private void Button2_Click(object sender, EventArgs e)
{
this.Hide();
Form3 fm2 = new Form3();
fm2.Show();
}
private void Chkbox_showHide2_CheckedChanged(object sender, EventArgs e)
{
//Show/Hide password text in Manager Textbox
if (chkbox_showHide2.Checked)
{
txt_txtmanAvpass.UseSystemPasswordChar = true;
}
else
{
txt_txtmanAvpass.UseSystemPasswordChar = false;
}
}
private void TxtClip1_Click(object sender, EventArgs e)
{
//Copy textbox to clipboard
Clipboard.SetText(txt_ManPass.Text);
}
private void TxtClip2_Click(object sender, EventArgs e)
{
//Copy textbox to clipboard
Clipboard.SetText(txt_txtmanAvpass.Text);
}
private void BtnQuit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void BtnUpdate_Click(object sender, EventArgs e)
{
string test = txt_siteName.Text;
DialogResult dialogResult = MessageBox.Show("Are you sure you want to edit " + test, "You are about to update a site!" ,MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
string cs = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf";
//SQLconnection
SqlConnection con = new SqlConnection(cs);
con.Open();
string query = "UPDATE AvSites SET projectNr=@ProjectNr, siteName=@siteName, siteLicenses=@siteLicenses, siteClients=@siteClients, siteManagerIP=@siteManagerIP, siteManagerPassword=@siteManagerPassword, siteManagerAvUser=@siteManagerAvUser, SiteManagerAvPass=@SiteManagerAvPass, siteLicenseExpi=@siteLicenseExpi, siteAvMove=@siteAvMove, siteAvTools=@siteAvTools, siteManagerUser=@siteManagerUser WHERE id=@id";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@projectNr", SqlDbType.VarChar).Value = txt_siteProjnr.Text;
cmd.Parameters.AddWithValue("@siteName", SqlDbType.VarChar).Value = txt_siteName.Text;
cmd.Parameters.AddWithValue("@siteClients", SqlDbType.VarChar).Value = txt_siteClients.Text;
cmd.Parameters.AddWithValue("@siteLicenses", SqlDbType.VarChar).Value = txt_siteLicenses.Text;
cmd.Parameters.AddWithValue("@siteManagerIP", SqlDbType.VarChar).Value = txt_siteManaip.Text;
cmd.Parameters.AddWithValue("@siteManagerPassword", SqlDbType.VarChar).Value = txt_ManPass.Text;
cmd.Parameters.AddWithValue("@siteManagerAvUser", SqlDbType.VarChar).Value = txt_manAvusr.Text;
cmd.Parameters.AddWithValue("@SiteManagerAvPass", SqlDbType.VarChar).Value = txt_txtmanAvpass.Text;
cmd.Parameters.AddWithValue("@SiteLicenseExpi", SqlDbType.VarChar).Value = txt_siteLicenseExpi.Text;
cmd.Parameters.AddWithValue("@siteManagerUser", SqlDbType.VarChar).Value = txt_siteManUsr.Text;
cmd.Parameters.AddWithValue("@siteAvMove", SqlDbType.VarChar).Value = txt_siteAvMove.Text;
cmd.Parameters.AddWithValue("@siteAvTools", SqlDbType.VarChar).Value = txt_siteAvTools.Text;
cmd.Parameters.AddWithValue("@id", SqlDbType.VarChar).Value = txt_siteID.Text; //invisible textbox we use this to determine the site
cmd.ExecuteNonQuery();
con.Close();
FrmMain fm2 = new FrmMain();
fm2.Show();
this.Close();
}
else if (dialogResult == DialogResult.No)
{
//do nothing
}
}
private void TxtClip3_Click(object sender, EventArgs e)
{
Clipboard.SetText(txt_siteManUsr.Text);
}
}
}