Reputation: 734
in my application i have two forms.one for login another for actual application. but after successful login database connection made by login form does not terminate. only way to close this connection is close both forms. but i want to close database connection made by login form as soon as successful login. here's my code
private void button2_Click(object sender, EventArgs e)
{
try
{
string mysqlconnection = string.Format("datasource='{0}';username=uwcentrallogin;port=3306;password=**************;Connect Timeout=20000;Command Timeout=28800", serverip.Text);
MySqlConnection myconn = new MySqlConnection(mysqlconnection);
MySqlCommand Selectcommand = new MySqlCommand("select * from wartif.userdata where username='" + this.adminusername.Text.Trim() + "'and adminpassword= '" + this.passwordtext.Text.Trim() + "' ; ", myconn);
MySqlDataReader myreader;
myconn.Open();
myreader = Selectcommand.ExecuteReader();
int count = 0;
while (myreader.Read())
{
count = count + 1;
}
if (count == 1)
{
this.Hide();
adminview f2 = new adminview(serverip.Text, adminusername.Text, portnumberbox.Text, defdatabase.Text);
f2.ShowDialog();
this.Close();
myconn.Close();
}
else if (count > 1)
{
label4.Text = "duplicatie users exsist ";
}
else
label4.Text = "Not a privileged user";
label4.ForeColor = Color.Orange;
errpan.BackColor = Color.Orange;
myconn.Close();
}
catch
{
label4.Text = "mysql database connection is not avialable";
}
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void button4_Click(object sender, EventArgs e)
{
appconfigsave();
label4.Text = "Your changes has been saved";
label4.ForeColor = Color.Orange;
errpan.BackColor = Color.Orange;
}
Upvotes: 0
Views: 382
Reputation: 18827
add:
myconn.Dispose();
below your line:
myconn.Close();
I would also advise you look at Using
Statments
More info here
Example
if (count == 1)
{
this.Hide();
adminview f2 = new adminview(serverip.Text, adminusername.Text, portnumberbox.Text, defdatabase.Text);
myconn.Close();
myconn.Dispose();
f2.ShowDialog();
this.Close();
}
Upvotes: 1