请查看我的代码。 [英] please just review my code.

查看:103
本文介绍了请查看我的代码。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是asp.net的新手,目前正在做我的实习生。我被要求开发一个应用程序,但我似乎无法让我的代码工作。请查看我的代码并告诉我我做错了什么。我不需要答案,只知道我做错了什么。



这是我的商业逻辑层





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


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

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





我的业务对象没问题,所以我不会打扰你们。

这里ismy aspx





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

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

推荐答案

请注意我的代码不是很好而且非常基本但请记住,它是我第三天只学习代码
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


这篇关于请查看我的代码。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆