Results 1 to 4 of 4

Thread: Error code in C#

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    73

    Error code in C#

    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

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Error code in C#

    You need to be sightly more specific. What sort of connection? And can you post the code you're using to do it?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2006
    Posts
    73

    Re: Error code in C#

    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";
    }

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Error code in C#

    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.
    Code:
    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();
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width