Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi guys, I am new at asp.net and currently doing my intern. I am asked to developed an app but i can't seem to get my code to work. Please just review my code and tell me what i did wrong. I dont need the answers, just to know where i did wrong.

This is my business logic layer


C#
public class expensebl
    {
        protected ExpenseDA expenseda;
       
        public expensebl()
        {
            //error here
            expenseda = new ExpenseDA(ConstantHelper.AppSettings.ConnectionString);      
        }

        public void SubmitExpense (expense obj)
        {
            //if else statement not needed as expense tracker can have multiple same entry
           // expenseda = new ExpenseDA(ConstantHelper.AppSettings.ConnectionString);
            expenseda.CreateExpense(obj);
        }

        public List<expense> GetAllExpense()
        {
            try
            {
                return expenseda.GetAllExpense();
            }
            catch {}
            return null;
        }



Data Access layer

C#
public class ExpenseDA // to enable access by others
{
    private string connStr;

    //constructor
    public ExpenseDA(string ConnectionString)
    {
        connStr = ConnectionString;
    }

    public void CreateExpense(expense obj)
    {
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            try
            {
                //initiate sql command
                SqlCommand cmd = new SqlCommand();
                //passing of query
                cmd.Connection = conn;
                cmd.CommandText = "InsertExpense";
                // cmd.CommandType = CommandType.StoredProcedure (not added due to unable to create database)

                //connection open
                conn.Open();

                cmd.Parameters.AddWithValue("@Title", obj.Title);
                cmd.Parameters.AddWithValue("@Date", obj.Date);
                cmd.Parameters.AddWithValue("@Category", obj.Category);
                cmd.Parameters.AddWithValue("@Amount", obj.Amount);

                //execute command
                object returnedIdentity = cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to insert");
            }
            finally
            {
                conn.Close();
            }
        }
    }

        public List<expense> GetAllExpense()
        {
            DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "GettAllExpense";
                    cmd.CommandType = CommandType.StoredProcedure; //(not added due to unable to create database)
                    conn.Open();

                    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
                    adpt.Fill(dt);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }

            if (dt.Rows.Count > 0)
            {
                List<expense> lst = new List<expense>();

                foreach (DataRow dr in dt.Rows)
                {
                    expense obj = new expense();
                    obj.Title = Convert.ToString(dr[ConstantHelper.Column.Expense.Title]);
                    obj.Date = Convert.ToString(dr[ConstantHelper.Column.Expense.Date]);
                    obj.Category = Convert.ToString(dr[ConstantHelper.Column.Expense.Category]);
                    obj.Amount= Convert.ToDouble(dr[ConstantHelper.Column.Expense.Amount]);

                    lst.Add(obj);
                }
                return lst;
            }
           return null;
        }


my business object is ok so i will not bother you guys with it.
here ismy aspx

C#
public partial class expensetracker : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        //protected void Page_Load(expense obj)
        {
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           // string connStr = ConfigurationManager.ConnectionStrings["C']
            expensebl eBL = new expensebl();
            Response.Write("ok");
        }
Posted
Updated 11-Dec-14 17:13pm
v2
Comments
Shweta N Mishra 12-Dec-14 3:29am    
what is the problem you are facing and on which line

1 solution

pls note my code isn't as good and it is quite basic but please bear in mind, it is my 3rd day only learning the code
 
Share this answer
 
Comments
Shweta N Mishra 12-Dec-14 3:29am    
You should post your commments in question or on the ansers given by others, I you do not have the answer. Delete this other wise someone will report it "Not an Answer" and you will be downvoted for that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900