hello every programmers.
I am new in OOP and I am writing an application that works with db.(insert , update , delete in db).
I have used DAL and BLL and View Layers for it.
DAL Class:
public class Mat_Issue
{
public short ItemsId { get; set; }
public short ReportId { get; set; }
public int MtoId { get; set; }
public decimal Qty { get; set; }
public string? Remark { get; set; }
public string? SubName { get; set; }
public byte? Wid { get; set; }
}
public static class Mat_IssueDAL
{
public static string Error { get; set; }
public static List<Mat_Issue> GetAll()
{
List<Mat_Issue> list = new();
try
{
string query = "SELECT * FROM [dbo].[Mat_Issue]";
using SqlCommand cmd = new SqlCommand(query, Connection.Conn);
Connection.Open();
using SqlDataReader rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read()){
Mat_Issue obj = new();
obj.ItemsId = rd.GetFieldValue<short>("ItemsId");
obj.ReportId = rd.GetFieldValue<short>("ReportId");
obj.MtoId = rd.GetFieldValue<int>("MtoId");
obj.Qty = rd.GetFieldValue<decimal>("Qty");
obj.Remark = rd.GetFieldValue<string>("Remark");
obj.SubName = rd.GetFieldValue<string>("SubName");
obj.Wid = rd.GetFieldValue<byte>("Wid");
list.Add(obj);
}
}
}
catch (Exception ex)
{
Error = ex.Message;
}
finally{
Connection.Close();}
return list;
}
public static int Delete(short ItemsId)
{
int result = 0;
try
{
SqlCommand cmd = new SqlCommand("DELETE FROM [dbo].[Mat_Issue] WHERE ItemsId=@ItemsId", Connection.Conn);
cmd.Parameters.AddWithValue("@ItemsId", ItemsId);
Connection.Open();
result = cmd.ExecuteNonQuery();
Connection.Close();}
catch (Exception ex)
{Error = ex.Message; }
finally {
Connection.Close();
}
return result; }
public static int Add(Mat_Issue obj)
{
int result = 0;
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[Mat_Issue] VALUES (@ReportId,@MtoId,@Qty,@Remark,@SubName,@Wid )", Connection.Conn);
cmd.Parameters.AddWithValue("@ReportId", obj.ReportId);
cmd.Parameters.AddWithValue("@MtoId", obj.MtoId);
cmd.Parameters.AddWithValue("@Qty", obj.Qty);
cmd.Parameters.AddWithNullValue("@Remark", obj.Remark);
cmd.Parameters.AddWithNullValue("@SubName", obj.SubName);
cmd.Parameters.AddWithNullValue("@Wid", obj.Wid);
Connection.Open();
result = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Error = ex.Number == 2627 ? "Duplicate Data" : ex.Message;
}
finally
{
Connection.Close();}
return result;
}
public static int Update(Mat_Issue obj)
{
int result = 0;
try {
SqlCommand cmd = new SqlCommand("UPDATE Mat_Issue SET Qty = @Qty,Remark = @Remark,SubName = @SubName WHERE ItemsId=@ItemsId", Connection.Conn);
cmd.Parameters.AddWithValue("@ItemsId", obj.ItemsId);
cmd.Parameters.AddWithValue("@ReportId", obj.ReportId);
cmd.Parameters.AddWithValue("@MtoId", obj.MtoId);
cmd.Parameters.AddWithValue("@Qty", obj.Qty);
cmd.Parameters.AddWithNullValue("@Remark", obj.Remark);
cmd.Parameters.AddWithNullValue("@SubName", obj.SubName);
Connection.Open();
result = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Error = ex.Number == 2627 ? "Duplicate Data" : ex.Message;
}
finally
{
Connection.Close();
}
return result;
} }
AND BLL:
public class Mat_IssueBLL
{
public static string GetError()
{
return Mat_IssueDAL.Error;
}
public static void SetError(string msg = "")
{
Mat_IssueDAL.Error = msg;
}
public static List<Mat_Issue> GetAll()
{
return Mat_IssueDAL.GetAll();
}
public static bool Delete(short itemsId)
{
return Mat_IssueDAL.Delete(itemsId) == 1 ? true : false;
}
public static bool Add(Mat_Issue obj)
{
return Mat_IssueDAL.Add(obj) > 0 ? true : false;
}
public static bool Update(Mat_Issue obj)
{
return Mat_IssueDAL.Update(obj) > 0 ? true : false;
}
}
View:(form control)
I read from DataGridView:
int _add = 0;
int _notAdd = 0;
Mat_Issue _issue = new();
foreach (DataGridViewRow row in DG_MtoIssue.Rows)
{
if (row.Cells[0].GetType() == typeof(DataGridViewCheckBoxCell))
{
DataGridViewCheckBoxCell? chk = row.Cells[0] as DataGridViewCheckBoxCell;
if (chk != null)
{
if (chk.Value == chk.TrueValue)
{
int.TryParse(row.Cells["MtoID"].Value.ToString(), out int mtoId);
decimal.TryParse(row.Cells["Siv_Qty"].Value.ToString(), out decimal qty);
byte.TryParse(row.Cells["Wid"].Value.ToString(), out byte wid);
_issue.ReportId = _report.ReportId;
_issue.MtoId = mtoId;
_issue.Qty = qty;
_issue.Wid = wid;
_issue.Remark = row.Cells["Remark"].Value.ToString();
_issue.SubName = row.Cells["SubName"].Value.ToString();
if (Mat_IssueBLL.Add(_issue))
_add++;
else
_notAdd++;
}
}
}
}
I want every body comment on my code.
1:are they standard?
2:should I dispose my class after doing CRUD operation? for example when user submit 1000 record the for loop call add 1000 times and add open db connection 100 times and close it 1000 times.
3:Can I move my view code into BLL layer to reduce code in view?