public List <clsUnitCategory> GetAllUnitCategoryInfo() { List <clsUnitCategory> lst = new List <clsUnitCategory>(); using (SqlConnection conn = new SqlConnection(strConn)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("TMR_USP_GetUnitCategory")) { cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = conn; SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { var info = new clsUnitCategory(); info.strUnitCategoryID = reader["UnitCategoryID"].ToString(); info.strDesc = reader["UnitCategoryDescription"].ToString(); info.id = Convert.ToInt32(reader["ID"]); lst.Add(info); } conn.Close(); } } return(lst); }
public bool AddUnitCategory(clsUnitCategory info) { int st = 0; try { using (SqlConnection conn = new SqlConnection(strConn)) { conn.Open(); { using (SqlCommand cmd = new SqlCommand("TMR_USP_AddUnitCategory")) { cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = conn; cmd.Parameters.AddWithValue("@UnitCategoryID", info.strUnitCategoryID); cmd.Parameters.AddWithValue("@UnitCategoryDescription", info.strDesc); cmd.Parameters.AddWithValue("@ID", info.id); st = cmd.ExecuteNonQuery(); } } conn.Close(); } } catch (Exception ex) { } if (st > 0) { return(true); } else { return(false); } }
private void btnAdd_Click(object sender, EventArgs e) { if (txtID.Text.Trim() == string.Empty) { MessageBox.Show("Unit Category id cannot be empty."); txtID.Focus(); return; } if (txtDesc.Text.Trim() == "") { MessageBox.Show("Description cannot be empty."); txtDesc.Focus(); return; } foreach (DataGridViewRow dgr in dgList.Rows) { if (dgr.Cells["UnitCategoryID"].Value.ToString().Trim().ToUpper() == txtID.Text.Trim().ToUpper() && this.id != Convert.ToInt32(dgr.Cells["ID1"].Value)) { MessageBox.Show("A unit category with same ID already exists."); return; } } clsUnitCategory nature = new clsUnitCategory(); nature.strUnitCategoryID = txtID.Text.Trim(); nature.strDesc = txtDesc.Text.Trim(); nature.id = this.id; bool result = da.AddUnitCategory(nature); if (result == false) { MessageBox.Show("An error occurred."); return; } LoadProjects(); Clear(); }