Click here to Skip to main content
15,907,231 members
Home / Discussions / C#
   

C#

 
GeneralRe: Try catch finally Pin
Claudiu Schiopu16-Dec-12 8:02
Claudiu Schiopu16-Dec-12 8:02 
Thank you for your message!

Beceause now i try to learn some C# programming (and write in english), your message help me to understand about Try catch.

Just to be sure that i have understand:

1. I have my btnSave code that look like this:

C#
protected void btnSave_Click(object sender, EventArgs e)
       {
           try
           {
               int x = 0; //this will be the ID of the new inserted record 
               x = SaveData();
               if (x > 0)
               {
 lblMesaje.Text = " Your new ID is: " + x.ToString();
               }
           }
           catch (Exception ex)
           {
               lblMesage.Text = "Some error occured: " + ex.Message;
           }

       }


2. Now here is my Insert function:

private int SaveData()
        {
         //SQL objects set..

            int result= 0;
            try
            {
                if (sqlconn.State != ConnectionState.Open)
                {
                    sqlconn.Open();
                }
                result= Convert.ToInt32(cmd.ExecuteScalar());
                return result;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (sqlconn.State != ConnectionState.Closed)
                {
                    sqlconn.Close();
                }
            }
            
        }


Now i throw and exception in SaveData function and the message is displayed to the user. Is ok?

Thanks again!
GeneralRe: Try catch finally Pin
OriginalGriff16-Dec-12 8:37
mveOriginalGriff16-Dec-12 8:37 
GeneralRe: Try catch finally Pin
dybs24-Dec-12 7:43
dybs24-Dec-12 7:43 
QuestionCalling powershell from C# Pin
MyTechnet15-Dec-12 17:42
MyTechnet15-Dec-12 17:42 
AnswerRe: Calling powershell from C# Pin
Richard MacCutchan15-Dec-12 21:08
mveRichard MacCutchan15-Dec-12 21:08 
GeneralRe: Calling powershell from C# Pin
c242317-Dec-12 23:55
c242317-Dec-12 23:55 
GeneralRe: Calling powershell from C# Pin
Richard MacCutchan18-Dec-12 0:35
mveRichard MacCutchan18-Dec-12 0:35 
AnswerRe: Calling powershell from C# Pin
c242317-Dec-12 23:52
c242317-Dec-12 23:52 
Question.net Web application Pin
vr99999999915-Dec-12 0:34
vr99999999915-Dec-12 0:34 
AnswerRe: .net Web application Pin
OriginalGriff15-Dec-12 3:37
mveOriginalGriff15-Dec-12 3:37 
QuestionReceivedBytesThreshold Pin
C-P-User-314-Dec-12 10:19
C-P-User-314-Dec-12 10:19 
AnswerRe: ReceivedBytesThreshold Pin
vr99999999915-Dec-12 0:57
vr99999999915-Dec-12 0:57 
QuestionRegex Pin
loctrice14-Dec-12 8:40
professionalloctrice14-Dec-12 8:40 
AnswerRe: Regex Pin
PIEBALDconsult14-Dec-12 8:46
mvePIEBALDconsult14-Dec-12 8:46 
GeneralRe: Regex Pin
loctrice14-Dec-12 9:10
professionalloctrice14-Dec-12 9:10 
AnswerRe: Regex Pin
loctrice14-Dec-12 9:10
professionalloctrice14-Dec-12 9:10 
GeneralLocation of Ascii table or ascii table generator Pin
computerpublic14-Dec-12 6:23
computerpublic14-Dec-12 6:23 
GeneralRe: Location of Ascii table or ascii table generator Pin
Ennis Ray Lynch, Jr.14-Dec-12 9:14
Ennis Ray Lynch, Jr.14-Dec-12 9:14 
GeneralRe: Location of Ascii table or ascii table generator Pin
computerpublic14-Dec-12 9:56
computerpublic14-Dec-12 9:56 
GeneralRe: Location of Ascii table or ascii table generator Pin
Ravi Bhavnani14-Dec-12 10:01
professionalRavi Bhavnani14-Dec-12 10:01 
GeneralRe: Location of Ascii table or ascii table generator Pin
computerpublic14-Dec-12 10:05
computerpublic14-Dec-12 10:05 
GeneralRe: Location of Ascii table or ascii table generator Pin
Ravi Bhavnani14-Dec-12 10:10
professionalRavi Bhavnani14-Dec-12 10:10 
GeneralRe: Location of Ascii table or ascii table generator Pin
computerpublic14-Dec-12 10:16
computerpublic14-Dec-12 10:16 
GeneralRe: Location of Ascii table or ascii table generator Pin
Ravi Bhavnani14-Dec-12 10:32
professionalRavi Bhavnani14-Dec-12 10:32 
GeneralRe: Location of Ascii table or ascii table generator Pin
Eddy Vluggen14-Dec-12 10:34
professionalEddy Vluggen14-Dec-12 10:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.