Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
C

Claudiu Schiopu

@Claudiu Schiopu
About
Posts
2
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Try catch finally
    C Claudiu Schiopu

    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:

    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!

    C# database help question

  • Try catch finally
    C Claudiu Schiopu

    Hi, Maby this is simple for you, but for me is not. I have this code:

    int rezultat = 0;
    try
    {
    if (sqlconn.State != ConnectionState.Open)
    {
    sqlconn.Open();
    }
    rezultat = (int)cmd.ExecuteScalar();

           }
           catch (Exception ex)
           {
               lblMesaje.Text = "Eroare: " + ex.Message.ToString();
    
           }
           finally
           {
               if (sqlconn.State != ConnectionState.Closed)
               {
                   sqlconn.Close();
               }
           }
    
           return rezultat;
    

    Is just for inserting a new record in a table. Even if this throw an error "Specified cast is not valid." "rezultat=(int)cmd.ExecuteScalar();" - the code is executed and the row is inserted in the database, and the execution continues. Why it continues? Maby i don't understand the try catch finally yet :) Thank you!

    C# database help question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups