Following my earlier comment, here is a formatted version of your code wherein I use the using statement.
protected void ImgLogin_Click ( object sender,
ImageClickEventArgs e )
{
try
{
string command_string;
string connection_string;
command_string = "SELECT * " +
"FROM tbl_login_user " +
"WHERE username=@username AND " +
"password=@password";
connection_string = ConfigurationManager.
ConnectionStrings [ "constrings" ].
ConnectionString;
using ( SqlConnection SQL_connection =
new SqlConnection (
connection_string ) )
{
SQL_connection.Open ( );
using ( SqlCommand SQL_command =
new SqlCommand (
command_string,
SQL_connection ) )
{
SQL_command.Parameters.AddWithValue (
"@username",
txtusername.Text );
SQL_command.Parameters.AddWithValue (
"@password",
txtpwduser.Text);
using ( SqlDataReader SQL_data_reader =
SQL_command.
ExecuteReader ( ) )
{
SQL_data_reader.Read ( );
if ( SQL_data_reader.HasRows )
{
Response.Redirect (
"~/Adminlogin/home.aspx",
false);
}
else
{
msg.ShowAlertMessage (
"Invalid Login Credentials" );
}
}
}
}
}
catch ( Exception ex )
{
Response.Write ( ex.ToString ( ) );
}
}
SQL_connection, SQL_command, and SQL_data_reader will all be closed at the end of each's using statement block.
In place of the using statements, you could use a single finish block to insure that all object opened in the try block are closed.