hey.........i m new in asp.net ....and following is the class library.... ........can somebody tell me the working of the following code like why catch{} is used why finally is used and all this

public abstract class clscon
    {
        protected SqlConnection con = new SqlConnection();
        public clscon()
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
        }
    }
    public class clsproduct : clscon
    {
        public void ins_product(clsproductprp objprp)
        { 
            SqlCommand inscmd = new SqlCommand();
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
               
                inscmd.CommandText = "spInsProduct";
                inscmd.CommandType = CommandType.StoredProcedure;
                inscmd.Connection = con;
                inscmd.Parameters.AddWithValue("@fancategoryid", objprp.categoryidprp);
                inscmd.Parameters.AddWithValue("@fsProductname", objprp.productnameprp);
                inscmd.Parameters.AddWithValue("@fanunitPrice", objprp.unitPriceprp);
                inscmd.Parameters.AddWithValue("@fanunitinstock", objprp.unitinStockprp);
                inscmd.Parameters.AddWithValue("@fanQuantityperunit", objprp.Quantityperunitprp);
                inscmd.ExecuteNonQuery();
           
            }
            catch
            {
            
            
            }
            finally 
            {
                inscmd.Dispose();
            }
        }

The Try Catch Finally is a way to handle errors in a good way. :)
When you are running a pice of code you ask it to TRY if everything works as it should then the code is ran then the FINALLY statement at the end runs. This is where you would close connections, readers, and whatnot. How it when you ask it to TRY and something goes wrong then the CATCH is fired where you have the chance to do something so that the user doesn't get an error bad looking error message. It then moves to the FINALLY there you would want to close things like connection, readers, and whatnot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.