Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public OleDbConnection conn;
public OleDbDataAdapter da;
public OleDbDataReader reader;
public DataSet ds;
public DataTable dt;
public OleDbCommand cmd;

try
         {

             cmd = new OleDbCommand("UPDATE tbl_user_login SET password='" + txt_password.Text + "' WHERE username='" + txt_username.Text + "' ", conn);
             conn.Open();
             cmd.CommandType = CommandType.Text;
             OleDbDataAdapter da = new OleDbDataAdapter(cmd);
             cmd.CommandType = CommandType.Text;
             cmd.ExecuteNonQuery();
             MessageBox.Show("updated successfully");
         }
         catch (Exception ex)
         {
             MessageBox.Show((ex.ToString()));
         }
         finally
         {
             if(conn.State == ConnectionState.Open)
             {
                 conn.Close();
             }
         }


ERROR--connection was not closed current state is open AND update syntax error
Posted
Updated 15-Apr-13 22:26pm
v3
Comments
dan!sh 16-Apr-13 4:17am    
What's the question here?
Malli_S 16-Apr-13 4:34am    
What exactly do you wanna ask?

C#
 public OleDbConnection conn;
  public OleDbDataAdapter da;
  public OleDbDataReader reader;
  public DataSet ds;
  public DataTable dt;
  public OleDbCommand cmd;
 
  try
           {
 
               cmd = new OleDbCommand("UPDATE tbl_user_login SET password='" + txt_password.Text + "' WHERE username='" + txt_username.Text + "' ", conn);
               if(conn.State != ConnectionState.Open)
                {
                conn.Open();
                }

               cmd.CommandType = CommandType.Text;
               OleDbDataAdapter da = new OleDbDataAdapter(cmd);
               cmd.CommandType = CommandType.Text;
               cmd.ExecuteNonQuery();
               MessageBox.Show("updated successfully");
           }
           catch (Exception ex)
           {
               MessageBox.Show((ex.ToString()));
           }
           finally
           {
               if(conn.State == ConnectionState.Open)
               {
                   conn.Close();
               }
           }

Happy Coding!
:)
 
Share this answer
 
Your problem is, that your db access objects are changed somewhere else, and you don't handle this situation.

Don't use class fields for this purpose. Use "using" statement instead when possible, and local variables. You get only trouble this way, you have no performance gain here.
Please read and follow best practices: http://msdn.microsoft.com/en-us/library/ms971481.aspx[^]
 
Share this answer
 

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