//Convert DataTable To object method public labourClass ConvertToOjbect(DataTable dt) { labourClass obj_labour = new labourClass(); for (int i = 0; i < dt.Rows.Count; i++) { if (Convert.ToString(dt.Rows[i]["Labourid"]) != "") { obj_labour.Labourid = Convert.ToInt32(dt.Rows[i]["Labourid"]); } else { obj_labour.Labourid = Convert.ToInt32("0"); } if (Convert.ToString(dt.Rows[i]["Labourname"]) != "") { obj_labour.Labourname = Convert.ToString(dt.Rows[i]["Labourname"]); } else { obj_labour.Labourname = Convert.ToString(""); } } return(obj_labour); }
//Convert DataTable To List method public List <labourClass> ConvertToList(DataTable dt) { List <labourClass> labourlist = new List <labourClass>(); for (int i = 0; i < dt.Rows.Count; i++) { labourClass obj_labour = new labourClass(); if (Convert.ToString(dt.Rows[i]["Labourid"]) != "") { obj_labour.Labourid = Convert.ToInt32(dt.Rows[i]["Labourid"]); } else { obj_labour.Labourid = Convert.ToInt32("0"); } if (Convert.ToString(dt.Rows[i]["Labourname"]) != "") { obj_labour.Labourname = Convert.ToString(dt.Rows[i]["Labourname"]); } else { obj_labour.Labourname = Convert.ToString(""); } labourlist.Add(obj_labour); } return(labourlist); }
//create parameter public void createParameter(labourClass obj, DB_con.DBTrans trans) { try { obj_con.clearParameter(); obj_con.addParameter("@Labourname", string.IsNullOrEmpty(Convert.ToString(obj.Labourname)) ? "" : obj.Labourname); obj_con.addParameter("@Labourid", obj.Labourid, trans); } catch (Exception ex) { throw ex; } }
//Select Constructor public labourCtl(Int32 id) { obj_con = new ConnectionCls(); labourClass obj_lab = new labourClass(); using (DataTable dt = selectdatatable(id)) { if (dt.Rows.Count > 0) { obj_lab.Labourid = Convert.ToInt32(dt.Rows[0]["Labourid"]); obj_lab.Labourname = Convert.ToString(dt.Rows[0]["Labourname"]); } } }
//insert data into database public Int32 insert(labourClass obj) { try { obj_con.clearParameter(); createParameter(obj, DBTrans.Insert); obj_con.BeginTransaction(); obj_con.ExecuteNoneQuery("sp_labour_insert", CommandType.StoredProcedure); obj_con.CommitTransaction(); return(obj.Labourid = Convert.ToInt32(obj_con.getValue("@Labourid"))); } catch (Exception ex) { obj_con.RollbackTransaction(); throw new Exception("sp_labour_insert"); } }
//update edited object public labourClass updateObject(labourClass obj) { try { labourClass oldObj = selectById(obj.Labourid); if (obj.Labourname == null || obj.Labourname.ToString().Trim() == "update") { obj.Labourname = oldObj.Labourname; } return(obj); } catch (Exception ex) { throw new Exception(ex.Message); } }