Click here to Skip to main content
14,356,370 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100