Thursday, October 21, 2010

Check existing record in 3 tier architecture

Front-end / web page
if (isnew_record) {
            var _bll = new businesslogiclayer();
            if (_bll.CheckExistingRecord(txtReferenceNo.Text.ToString().Trim()) > 0) {
                lblError.Text = "Reference Number already exists.";
                return;
            }
        }

Business Logic Layer
 public int CheckExistingRecord(string refnumber) {
            var _dal = new dataacceslayer();
            return _dal.CheckExistingRecord(refnumber);
        }

Data Access Layer
internal int CheckExistingRecord(string refnumber) {
            base.com.CommandText = "select count(refnumber) from tablename where refnumber = @refnumber";
            base.com.Parameters.AddWithValue("@refnumber", refnumber);
            
            return Convert.ToInt32(base.com.ExecuteScalar());

        }

Deleting or removing row in datatable


DataRow theRowToDelete = theDataTable.Rows[Index];

theDataTable.Row.Remove(theRowToDelete);

Create datatable at runtime c#


//declares datatable
DataTable dt= new DataTable("parameter_final_result");
dt.Clear();
  
//delcares and add new datacolumn
DataColumn col1 = new DataColumn("parameter", Type.GetType("System.String"));
DataColumn col2= new DataColumn("rating", Type.GetType("System.String"));
DataColumn col3 = new DataColumn("material", Type.GetType("System.String"));
DataColumn col4 = new DataColumn("failed_result", Type.GetType("System.String"));
DataColumn col5 = new DataColumn("unit", Type.GetType("System.String"));
DataColumn col6 = new DataColumn("date_prepared", Type.GetType("System.DateTime"));
  
//add datacolumn to datatable
dt.Columns.Add(col1);
dt.Columns.Add(col2);
dt.Columns.Add(col3);
dt.Columns.Add(col4);
dt.Columns.Add(col5);
dt.Columns.Add(col6);