UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> C# equivalent to 'Exit Sub'    
 
   
kbrewster
post Nov 23 2009, 09:37 AM
Post #1

UtterAccess VIP
Posts: 4,489
From: NH



I have just finished a C# project and I am now going through and adding error trapping. But I have come across this one error...the code is calling a stored procedure that does not exist and the error keeps popping up after hitting OK and you have to go to task manager to exit out of the program. I am using the try, catch and finally but I want it to exit out of the code...
CODE
private void FillClientCodes()
        {
            //Gets a list of client codes from EMR Database
            lvClientCodes.Visible = true;
            lvClientCodes.Items.Clear();
            SqlConnection con = new SqlConnection(constring);
            SqlDataReader rdr = null;
            try
            {
                con.Open();

                SqlCommand cmd = new SqlCommand("EHR_GetClientCodes", con);
                cmd.CommandType = CommandType.StoredProcedure;

                rdr = (SqlDataReader)cmd.ExecuteReader();

                ListViewItem itmX;

                while (rdr.Read())
                {
                    itmX = new ListViewItem();
                    itmX.Text = rdr.GetValue(0).ToString();

                    for (int i = 1; i < rdr.FieldCount; i++)
                    {
                        itmX.SubItems.Add(rdr.GetValue(i).ToString());
                    }
                    lvClientCodes.Items.Add(itmX);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        finally
        {
            if (con != null)
            {
                con.Close();
            }
            if (rdr != null)
            {
                rdr.Close();
            }
        }
Go to the top of the page
 
+
rabroersma
post Nov 23 2009, 09:48 AM
Post #2

UtterAccess VIP
Posts: 1,215
From: Arcadia, California, USA



What exactly is the error that you are getting?

Also for a function returning void the equivalent to exit sub would be:

return;
Go to the top of the page
 
+
ace
post Nov 23 2009, 09:49 AM
Post #3

UtterAccess VIP
Posts: 5,280
From: Upstate NY, USA



Use your favorite search engine to serach for C# Return


Return is how you exit and return a value from a C# function.
Go to the top of the page
 
+
kbrewster
post Nov 23 2009, 09:53 AM
Post #4

UtterAccess VIP
Posts: 4,489
From: NH



The exact error that I am getting is...
QUOTE
"Could not find stored procedure 'EHR_GetClientCodes'"


And it is occuring on the line...
CODE
rdr = (SqlDataReader)cmd.ExecuteReader();


And using return; is not making the error go away...it just keeps cycling on the screen rather than just showing once.
Go to the top of the page
 
+
rabroersma
post Nov 23 2009, 10:02 AM
Post #5

UtterAccess VIP
Posts: 1,215
From: Arcadia, California, USA



Rather than the error message, what is the error class that is getting thrown?

The catch{} will trap the error once it is encountered. The only way I can see that you'd see this error message cycling would be if you are cycling the call to FillClientCodes().

It could be that trapping the error in FillClientCodes() is the wrong design. If it make sense to your logic, it may make your life easier if you throw this error back up to the calling function so that it can deal with it.


Also, the design of your application requires you to call stored procedures that may not exist, it may help to test for their existence first before calling the procedure.
Go to the top of the page
 
+
kbrewster
post Nov 23 2009, 10:10 AM
Post #6

UtterAccess VIP
Posts: 4,489
From: NH



Ok, so it keeps cycling because on the form's Activated event I am calling FillClientCodes() and therefore, each time I click the ok button, the form is activated. But I need the code to run on the activated event because from this form you can open another form, but once this second form is close I need the first form to 'refresh'.

How can I "throw this error back up to the calling function so that it can deal with it"?
Go to the top of the page
 
+
rabroersma
post Nov 23 2009, 10:28 AM
Post #7

UtterAccess VIP
Posts: 1,215
From: Arcadia, California, USA



QUOTE
How can I "throw this error back up to the calling function so that it can deal with it"?


To pass the exception to the calling function you use:

throw;

in the catch block.

Or if develop your own application specific error class hierarchy, you can throw your own application specific error class:

http://msdn.microsoft.com/en-us/library/1a...28VS.80%29.aspx


QUOTE
But I need the code to run on the activated event because from this form you can open another form, but once this second form is close I need the first form to 'refresh'.
There are different designs you can choose to accomplish this. One design that would make your application forms loosely coupled would be the use of the Observer design pattern..

I could create a linked-list that would store the results of the stored procedure (the observed subject). Any controls that are based on this observed list are notified anytime the list is changed so that they can make an appropriated action. Now all form from never need any code to keep the lists refreshed.

Edited by: rabroersma on Mon Nov 23 10:30:19 EST 2009.
Go to the top of the page
 
+
kbrewster
post Nov 23 2009, 10:40 AM
Post #8

UtterAccess VIP
Posts: 4,489
From: NH



I just changed the FillClientCodes() from a private void to a public void and am calling it from the second form. Now everything seems to be working as expected..thanks!
Go to the top of the page
 
+
datAdrenaline
post Nov 23 2009, 10:52 AM
Post #9

UtterAccess Editor
Posts: 15,978
From: Northern Virginia, USA



If you do find out the exeception class, as Richard talked about, it is good to understand that you can have more than one catch block in order to suppress a specific exception that is being thrown ... something like this:

CODE
try
    {
        <your code>
    }
    catch (someexceptionclass ex)
    {
        //if the exception is the exception class you specify, then the ex is suppressed
        //and control is passed back to the caller.
        return;
    }
    catch (Exception ex)
    {
        //this will "Throw" the exception to the caller for handling
        throw new Exception("FillClientCodes():" + ex.message);
    }
Go to the top of the page
 
+
kbrewster
post Nov 23 2009, 11:26 AM
Post #10

UtterAccess VIP
Posts: 4,489
From: NH



Thank you Brent! I am sure this will come in handy! (IMG:http://www.utteraccess.com/forum/style_emoticons/default/sad.gif)
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 25th May 2013 - 03:04 AM