PDA

Click to See Complete Forum and Search --> : Error code in C#


longwar
May 17th, 2006, 10:20 PM
hi all,
i am still a novice in C#. i like to know how do i capture the error code in the event of error during connection. how do i capture this error code in integer? can someone pls help me with the codes? thks

longwar

penagate
May 17th, 2006, 10:53 PM
You need to be sightly more specific. What sort of connection? And can you post the code you're using to do it?

longwar
May 17th, 2006, 11:12 PM
let say i wanna add an error handlering codes. so that when there is a connection failure i can prompt the user about it. some thing like this

If ReceivedErrCode = 0 then
MsgBox "Login OK"
LoginSuccess
Elseif ReceivedErrCode = 1 then
MsgBox ("Error code : " & ReceiviedErrCode & " pls contact .... ")
End Sub
End If

the above is something i want to write in. but i dunno how to capture the error code. pls someone help me in this

my actual code is something like below

private void btnLogin_Click(object sender, System.EventArgs e)
{

// Password Length : 8 chars
// Robust Password :
// Lower Case, Upper Case, Numbers, Symbols
// e.g 1234@abc or Password1

//enforce password policy
if (this.txtPwd.Text.Length <=8)
{
MessageBox.Show ("The password must have at least 8 characters" , "Login");
return;
}

char[] charnumeral = new char[20] {'0','1','2','3','4','5','6','7','8','9', '~','!','@', '#', '$', '%', '^', '&', '*', '+'};

if (this.txtPwd.Text.IndexOfAny(charnumeral) <0 )
{
MessageBox.Show ("The password must contain numeric or symbol characters e.g 1234@abc or Password1. The symbol characters are ~ ! @ # $ % ^ & + " , "Login");
return;

}

UID = this.txtUserName.Text;
PWD = this.txtPwd.Text ;
// lblAuthenticate.Text = "Please wait while Authenticating ....";
// lblAuthenticate.Visible = true;
//
//Formulate message to be sent over using Listener instance

LoginSuccess();

}

private void LoginSuccess()
{
ProcessInfo pi = new ProcessInfo();
CreateProcess(@"\windows\pvbload.exe", @"\Program Files\LaunchPad.vb", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, new Byte[128], pi);

UserProfile.UpdateUserProfile(Name, Year, IP_Address, ID);

btnLogout.Visible = true;
btnCancel.Enabled = false;
txtPwd.Text = "";
txtPwd.Enabled = false;
this.Text = "Login - Connected";
}

jmcilhinney
May 17th, 2006, 11:35 PM
You still haven't told us where this error code is supposed to be coming from. Exception handling is performed in C# using Structured Exception Handling (SEH), which is implement using try...catch blocks. You place the code that you want to TRY to execute inside the 'try' block, then you CATCH whichever exceptions you're interested in in the 'catch' block. You can catch only some exceptions or all exceptions and you can handle them all the same way or you can handle some or all in different ways. You then place any code that must be executed whether an exception is thorwn or not into a 'finally' block. The finally block is optional and would be used for things like closing database connections or files.SqlConnection myConnection = new SqlConnection("connection string here.");
SqlCommand myCommand = new SqlCommand("SELECT COUNT(*) FROM MyTable", myConnection);

try
{
myConnection.Open();

int rowCount = (int)myCommand.ExecuteScalar();

MessageBox.Show("The tables contains " + rowCount.ToString() + " records.");
}
catch (InvalidOperationException e)
{
MessageBox.Show("No data source specified or the connection is already open.");
}
catch (SqlException e)
{
MessageBox.Show("An error occurred at the database.");
}
finally
{
myConnection.Close();
}