Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Fellow C#'ers

I need some help. I have a button were the text is Update on form load. When I press the button, it changes to Save. That works fine; however, I want to execute an Update Statement tot eh database when the text = update. I can't get that to work. I can either get the update statement to fire on the pressing of SAVE or not at all. Here's my code for the button.

C#
private void btnUpSave_Click( object sender, EventArgs e )
    {
    btnUpSave.Text = Resources.btnUpdateSaveTextSave;
    if ( _bln )
        BtnUpSaveEvent2();
    else
        BtnUpSaveEvent1();
    _bln = !_bln;

    Log.Info( "btnUpSave_Click Event saved for " + tbUserId.Text );
    }


What I need to do is execute the method UpdateUser() when text = Save... possible? thoughts? Hopes and prayers?

UpdateUser() code

C#
private void UpdateUser()
           {
           string userid = tbUserId.Text;
           string name = tbUserName.Text;
           string email = tbEmail.Text;
           string dob = tbDob.Text;
           string empid = tbEmpId.Text;
           int dc = cbActive.Checked ? 0 : 1;
           string hired = tbHireDate.Text;
           string start = tbStartDate.Text;
           string term = tbTermDate.Text;
           int gender = cboxMale.Checked ? 1 : 2;
           string addr1 = tbAddr1.Text;
           string addr2 = tbAddr2.Text;
           string zip = tbZip.Text;
           string phone = tbPhone.Text;
           string sp = Settings.Default.SqlUpdateUser;
           string spParams = sp + "@USERID = '" + userid + "' , @NAME = '" + name + "' ,  EMAIL = '" + email +"' , @DOB = '" + dob + "' , @EMPID = '" + empid + "' , @DC = '" + dc + "' , @HIRED = '" + hired + "' , @START = '" + start + "' , @TERM  = '" + term + "' , @GENDERID = '" + gender + "' , @ADDR1 = '" + addr1 + "' , @ADDR2 = '" + addr2 + "' , @ZIP = '" + zip + "' , @PHONE = '" + phone + "'";
           Log.Debug( "User Information Screen Executing SQL: " + spParams );
           _conString = Settings.Default.IMSConnectionString;
           Log.Info( "User Information Screen Using Database Connection: " + _conString );
           try
               {
               using ( var conn = new SqlConnection( _conString ) )
                   {
                   conn.Open();
                   using ( var command = new SqlCommand( spParams, conn ) )
                       {
                       //command.CommandType = CommandType.StoredProcedure;
                       command.ExecuteNonQuery();
                       }
                   }
               }
           catch ( SqlException ex )
               {
               string msg = ex.ToString();
               string title = Resources.CustomMsgBxTitle;
               MessageBoxManager.OK = "Upload";
               MessageBoxManager.Cancel = Resources.btnCloseCancelTextClose;
               MessageBoxManager.Register();

               DialogResult result = MessageBox.Show( msg, title, MessageBoxButtons.OKCancel, MessageBoxIcon.Hand );
               switch ( result )
                   {
                   case DialogResult.OK:
                       Clipboard.SetText( msg );
                       Process.Start( "web addr here" );
                       break;
                   case DialogResult.Cancel:
                       break;
                   }
               MessageBoxManager.Unregister();
               Log.Debug( "SQL Exception for : " + tbUserId.Text + "with exception message: " + ex );
               Log.Info( "Check SP " + sp + " - Executed as: " + spParams );
               }
           }
Posted
Updated 13-Sep-14 14:03pm
v2
Comments
Derek Kennard 13-Sep-14 20:04pm    
Forgot something:
private bool _bln = true;

1 solution

I'm an idiot... Sorry to bother you!

I added the UpdateUser() to BtnUpSaveEvent1() and now it works. :)

C#
private void BtnUpSaveEvent1()
    {
    tbAddr1.ReadOnly = true;
    tbAddr2.ReadOnly = true;
    tbCity.ReadOnly = true;
    tbEmail.ReadOnly = true;
    tbEmpId.ReadOnly = true;
    tbHireDate.ReadOnly = true;
    tbPhone.ReadOnly = true;
    tbPrimeLoc.ReadOnly = true;
    tbStartDate.ReadOnly = true;
    tbState.ReadOnly = true;
    tbTeamCode.ReadOnly = false;
    tbTermDate.ReadOnly = true;
    tbUserId.ReadOnly = true;
    tbUserName.ReadOnly = true;
    tbZip.ReadOnly = true;
    tbDob.ReadOnly = true;
    cbActive.Enabled = false;
    cboxMale.Enabled = false;
    cboxFemale.Enabled = false;
    btnUpSave.Text = Resources.btnUpdateSaveTextUpdate;
    btnCloseCancel.Text = Resources.btnCloseCancelTextClose;
    UpdateUser();
    }
 
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