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.
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
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.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 );
}
}