0

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();
            }
        }

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Freon22
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.