Click here to Skip to main content
14,691,183 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want insert value after close the form from close button , i used , this code but i have this exception.

i know what is mean this exception but how i make this code work,

private void FRM_Main_FormClosing(object sender, FormClosingEventArgs e)

s = " insert into LoginAndOutDate(user_name, Logout_Date) ";
s = s + " values(@user_name, CURRENT_TIMESTAMP) ";

if (con.State == ConnectionState.Closed)
con.Open();

sCommand = new SqlCommand(s, con);
sCommand.Parameters.AddWithValue("@user_name", textBox_ForName.Text);
sCommand.ExecuteNonQuery();

if (con.State == ConnectionState.Open)
con.Close();

this.Close();

this.RefToFormLogin.Show();

}

What I have tried:

i want when i close form from close button , the value insert..
Posted
Updated 7-Aug-16 3:36am

1 solution

This is because your are calling
this.Close();
in your
FRM_Main_FormClosing
which Triggers FRM_Main_FormClosing again ... which calls this.Close() again, ....

Conclution: Remove this.Close() in your FRM_Main_FormClosing Event handler.
   
Comments
MahmoudOmar 7-Aug-16 9:12am
   
it's work thanks, but i have new problem when i click on the button Log Out the insert happen twice because compiler insert to FRM_Main_FormClosing Event handler. too how i stop it and thanks
0x01AA 7-Aug-16 9:56am
   
You are welcome. Your new question: Sorry I don't understand what the Problem is. In case it is that you insert the logout allready in ButtonLogout you should capture this Information in something like private bool logoutLogged, which you initialize to false and set it to true in ButtonLogout. In FormClose you can check then wheter logout is allready registered or not. But I have a Feeling, that I don't understand this second question correctly :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900