How can I close my application when I got an error?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alpha

    How can I close my application when I got an error?

    I have a window Application. this.close(); doesn't stop and close my
    application when I get an error. Instead it continues trying to execute the
    following codes. What code do I need to the application to exit where it
    catches the exception and close down the application?

    Thanks, Alpha

    conDB = new SqlConnection(c onStr);

    try
    {
    conDB.Open();
    scUpdate = conDB.CreateCom mand();
    conDB.Close();
    }
    catch(Exception ex)
    {
    MessageBox.Show ("Error connecting to the database server. VMS can't run
    with out database connection and VMS will be closing down. " + ex.ToString()+
    "Please contact your support person.", "VMS - Database Open Error",
    MessageBoxButto ns.OK, MessageBoxIcon. Error);
    this.Close();

    }

  • Bob Powell [MVP]

    #2
    Re: How can I close my application when I got an error?

    Use Application.Exi t.

    --
    Bob Powell [MVP]
    Visual C#, System.Drawing

    Ramuseco Limited .NET consulting


    Find great Windows Forms articles in Windows Forms Tips and Tricks


    Answer those GDI+ questions with the GDI+ FAQ


    All new articles provide code in C# and VB.NET.
    Subscribe to the RSS feeds provided and never miss a new article.





    "Alpha" <Alpha@discussi ons.microsoft.c om> wrote in message
    news:C026193C-7595-42A4-9131-C6080806B3CF@mi crosoft.com...[color=blue]
    >I have a window Application. this.close(); doesn't stop and close my
    > application when I get an error. Instead it continues trying to execute
    > the
    > following codes. What code do I need to the application to exit where it
    > catches the exception and close down the application?
    >
    > Thanks, Alpha
    >
    > conDB = new SqlConnection(c onStr);
    >
    > try
    > {
    > conDB.Open();
    > scUpdate = conDB.CreateCom mand();
    > conDB.Close();
    > }
    > catch(Exception ex)
    > {
    > MessageBox.Show ("Error connecting to the database server. VMS can't run
    > with out database connection and VMS will be closing down. " +
    > ex.ToString()+
    > "Please contact your support person.", "VMS - Database Open Error",
    > MessageBoxButto ns.OK, MessageBoxIcon. Error);
    > this.Close();
    >
    > }
    >[/color]


    Comment

    • Peter Bromberg [MVP]

      #3
      Re: How can I close my application when I got an error?

      Use System.Environm ent.Exit
      Peter

      "Alpha" <Alpha@discussi ons.microsoft.c om> wrote in message
      news:C026193C-7595-42A4-9131-C6080806B3CF@mi crosoft.com...[color=blue]
      >I have a window Application. this.close(); doesn't stop and close my
      > application when I get an error. Instead it continues trying to execute
      > the
      > following codes. What code do I need to the application to exit where it
      > catches the exception and close down the application?
      >
      > Thanks, Alpha
      >
      > conDB = new SqlConnection(c onStr);
      >
      > try
      > {
      > conDB.Open();
      > scUpdate = conDB.CreateCom mand();
      > conDB.Close();
      > }
      > catch(Exception ex)
      > {
      > MessageBox.Show ("Error connecting to the database server. VMS can't run
      > with out database connection and VMS will be closing down. " +
      > ex.ToString()+
      > "Please contact your support person.", "VMS - Database Open Error",
      > MessageBoxButto ns.OK, MessageBoxIcon. Error);
      > this.Close();
      >
      > }
      >[/color]


      Comment

      • Alpha

        #4
        Re: How can I close my application when I got an error?

        It's asking for ExitCode, as System.Environm ent.Exit(ExitCo de), when I
        compile the program. What should I put in there? 0?
        Thanks.

        "Peter Bromberg [MVP]" wrote:
        [color=blue]
        > Use System.Environm ent.Exit
        > Peter
        >
        > "Alpha" <Alpha@discussi ons.microsoft.c om> wrote in message
        > news:C026193C-7595-42A4-9131-C6080806B3CF@mi crosoft.com...[color=green]
        > >I have a window Application. this.close(); doesn't stop and close my
        > > application when I get an error. Instead it continues trying to execute
        > > the
        > > following codes. What code do I need to the application to exit where it
        > > catches the exception and close down the application?
        > >
        > > Thanks, Alpha
        > >
        > > conDB = new SqlConnection(c onStr);
        > >
        > > try
        > > {
        > > conDB.Open();
        > > scUpdate = conDB.CreateCom mand();
        > > conDB.Close();
        > > }
        > > catch(Exception ex)
        > > {
        > > MessageBox.Show ("Error connecting to the database server. VMS can't run
        > > with out database connection and VMS will be closing down. " +
        > > ex.ToString()+
        > > "Please contact your support person.", "VMS - Database Open Error",
        > > MessageBoxButto ns.OK, MessageBoxIcon. Error);
        > > this.Close();
        > >
        > > }
        > >[/color]
        >
        >
        >[/color]

        Comment

        • Alpha

          #5
          Re: How can I close my application when I got an error?

          I tried the Application.Exi t in place of this.Close() but I got the same
          result. The application just continues with the codes and give more error
          message.

          Thanks, Alpha

          "Bob Powell [MVP]" wrote:
          [color=blue]
          > Use Application.Exi t.
          >
          > --
          > Bob Powell [MVP]
          > Visual C#, System.Drawing
          >
          > Ramuseco Limited .NET consulting
          > http://www.ramuseco.com
          >
          > Find great Windows Forms articles in Windows Forms Tips and Tricks
          > http://www.bobpowell.net/tipstricks.htm
          >
          > Answer those GDI+ questions with the GDI+ FAQ
          > http://www.bobpowell.net/faqmain.htm
          >
          > All new articles provide code in C# and VB.NET.
          > Subscribe to the RSS feeds provided and never miss a new article.
          >
          >
          >
          >
          >
          > "Alpha" <Alpha@discussi ons.microsoft.c om> wrote in message
          > news:C026193C-7595-42A4-9131-C6080806B3CF@mi crosoft.com...[color=green]
          > >I have a window Application. this.close(); doesn't stop and close my
          > > application when I get an error. Instead it continues trying to execute
          > > the
          > > following codes. What code do I need to the application to exit where it
          > > catches the exception and close down the application?
          > >
          > > Thanks, Alpha
          > >
          > > conDB = new SqlConnection(c onStr);
          > >
          > > try
          > > {
          > > conDB.Open();
          > > scUpdate = conDB.CreateCom mand();
          > > conDB.Close();
          > > }
          > > catch(Exception ex)
          > > {
          > > MessageBox.Show ("Error connecting to the database server. VMS can't run
          > > with out database connection and VMS will be closing down. " +
          > > ex.ToString()+
          > > "Please contact your support person.", "VMS - Database Open Error",
          > > MessageBoxButto ns.OK, MessageBoxIcon. Error);
          > > this.Close();
          > >
          > > }
          > >[/color]
          >
          >
          >[/color]

          Comment

          Working...