I have two forms: Form1 which is my app and Form2 which is a login page. I want to pass a value entered into the username textbox (LoginTbox) on Form2 to Form1. This is what I have so far. No error is received, but it seems to be passing nothing. I've tried constructors, but couldn't seem to get that to work either. What am i doing wrong?
Program.cs
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form2 fLogin = new Form2();
if (fLogin.ShowDialog() == DialogResult.OK)
Application.Run(new Form1());
else
Application.Exit();
}
Form2 (Login Form)
public string strVar = string.Empty;
public Form2()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
strVar = loginTbox.Text.ToString();
string _pass = textBox2.Text;
string conStr = "Data Source=CA-INVDEV\\RISEDB01;Initial Catalog=RISEDB01;Integrated Security=True";
string sqlcmd = "select * from accounts where Username=@Username and Password=@Password";
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlcmd, conn);
cmd.Parameters.AddWithValue("@Username", _username);
cmd.Parameters.AddWithValue("@Password", _pass);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
MessageBox.Show("Login Successful");
}
else
{
MessageBox.Show("Login Failed Invalid Credentials. Please try again");
Application.Restart();
}
}
}
Form1 (App)
private void button7_Click(object sender, EventArgs e)
{
if (textBox6.Text != "")
{
Form2 frm = new Form2();
string strValue = frm.strVar;
string Owner = textBox6.Text;
string Time = DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt");
string Serial = textBox4.Text;
string conStr = "Data Source=CA-INVDEV\\RISEDB01;Initial Catalog=RISEDB01;Integrated Security=True";
string sqlcmd2 = "Select * from Sheet1 where Serial#=@Serial#";
string sqlcmd = "UPDATE Sheet1 SET Owner=@Owner, Checked_In=NULL, Checked_Out=@Checked_Out, Modified_By=@Modified_By WHERE Serial#=@Serial#";
using (SqlConnection conn = new SqlConnection(conStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sqlcmd, conn);
SqlCommand cmd2 = new SqlCommand(sqlcmd2, conn);
cmd2.Parameters.AddWithValue("@Serial#", Serial);
cmd.Parameters.AddWithValue("@Serial#", Serial);
cmd.Parameters.AddWithValue("@Owner", Owner);
cmd.Parameters.AddWithValue("@Checked_Out", Time);
cmd.Parameters.AddWithValue("@Modified_By", strValue);
SqlDataReader dr = cmd2.ExecuteReader();
if (dr.HasRows)
{
dr.Close();
cmd.ExecuteNonQuery();
conn.Close();
Form1_Load();
}
else
{
dr.Close();
MessageBox.Show("Serial Does Not Exist");
textBox4.Clear();
}
}
}
else
{
MessageBox.Show("Owner was not assigned to asset. Please provide a Owner for this asset");
}
}
You're dealing with two completely separate instances of
Form2
. Your first instance, which the user used to login, is inaccessible from withinForm1
. The instance ofForm2
that you created inside the button click event inForm1
only has the initial value ofstring.Empty
stored instrVar
.To get it up and working, I'd change your Main method to pass the value you need into the constructor of
Form1
:And then modify the constructor of
Form1
(which I don't see in your snippet) to accept that argument:Get rid of the separate instance of
Form2
inside the button click event inForm1
.As a side FYI, if someone figures out how you're currently passing your textbox values to the database, they may be able to type something like
'; DELETE FROM ACCOUNTS;
intotextBox2.Text
and wreak havoc. (I haven't tried this specifically but something similar may work...)If you're curious at all, look up articles on SQL injection attacks, such as this one.