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()
{
expenseda = new ExpenseDA(ConstantHelper.AppSettings.ConnectionString);
}
public void SubmitExpense (expense obj)
{
expenseda.CreateExpense(obj);
}
public List<expense> GetAllExpense()
{
try
{
return expenseda.GetAllExpense();
}
catch {}
return null;
}
Data Access layer
public class ExpenseDA
{
private string connStr;
public ExpenseDA(string ConnectionString)
{
connStr = ConnectionString;
}
public void CreateExpense(expense obj)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "InsertExpense";
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);
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;
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 Button1_Click(object sender, EventArgs e)
{
expensebl eBL = new expensebl();
Response.Write("ok");
}