Thread: asp.net newbie

    #1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Dec 2014
    Posts
    1
    Rep Power
    0

    Unhappy asp.net newbie


    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.ConnectionStr ing);
    }

    public void SubmitExpense (expense obj)
    {
    //if else statement not needed as expense tracker can have multiple same entry
    // expenseda = new ExpenseDA(ConstantHelper.AppSettings.ConnectionStr ing);
    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;
    }

    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");
    }
  2. #2
  3. I do .NET for a living
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Sep 2003
    Location
    Florida
    Posts
    1,558
    Rep Power
    214
    Does the error occur when you compile or when you try to run the code? What is the error?
    Roger (.NET MCP)
  4. #3
  5. Contributing User
    ASP Skiller (1500 - 1999 posts)

    Join Date
    Aug 2008
    Location
    USA
    Posts
    1,606
    Rep Power
    347
    Code looks ok to me? do you have any issues in your code , any error ?
    if you found this post is useful click Give Rep button (bottom side on this reply ) and agree

    Jquery Shake Effect ,
    Andriod KitKat


    Thank You,
    KiranK

Similar Threads

  1. Newbie needs help
    By ribac in forum Microsoft Access Help
    Replies: 5
    Last Post: May 30th, 2006, 03:07 AM
  2. Newbie needs help
    By Jack_Doff in forum Microsoft SQL Server
    Replies: 0
    Last Post: April 14th, 2004, 05:05 AM
  3. Newbie to dot net
    By ASPjunkie in forum ASP Development
    Replies: 2
    Last Post: April 12th, 2004, 02:26 PM
  4. Newbie in need of help
    By andy11983 in forum ASP Development
    Replies: 2
    Last Post: April 9th, 2004, 01:32 AM
  5. Simple Newbie question (VERY NEWBIE)
    By mpetosky in forum ASP Development
    Replies: 1
    Last Post: February 13th, 2003, 06:27 PM

IMN logo majestic logo threadwatch logo seochat tools logo