public ActionResult AddEditAddressBook(AddressBookModel model) { if (ModelState.IsValid) //checking model is valid or not { DbData objDB = new DbData(); //calling class DBdata string result = string.Empty; if (model.ID.HasValue) { result = objDB.UpdateData(model); // passing Value to DBClass from model } else { result = objDB.InsertData(model); } ViewData["result"] = result; ModelState.Clear(); //clearing model return(RedirectToActionPermanent("ManageAddressBook")); // return View(); } else { // ModelState.AddModelError("", "Error in saving data"); return(View(new AddressBookModel())); } }
private bool UpdateDetail() { if (_sInsertDetail == string.Empty) { GenSqlString(); } bool success = false; foreach (DataRow drDetail in _lstDrCurrentDetails) { List <SqlField> tmp = new List <SqlField>(); List <string> paraNames = new List <string>(); List <object> paraValues = new List <object>(); List <SqlDbType> paraTypes = new List <SqlDbType>(); string sql = string.Empty; bool isDeleteDt = false, isDelete = false, updateIdentity = false; success = false; switch (drDetail.RowState) { case DataRowState.Added: if (_identityPkDt) { updateIdentity = true; } if (_identityPk) { string pk = _drTableMaster["Pk"].ToString(); if (drDetail.Table.Columns.Contains(pk)) { drDetail[pk] = _drCurrentMaster[pk]; } } tmp = _vInsertDetail; sql = _sInsertDetail; break; case DataRowState.Modified: tmp = _vUpdateDetail; sql = _sUpdateDetail; break; case DataRowState.Deleted: tmp = _vDeleteDetail; sql = _sDeleteDetail; if (_drCurrentMaster.RowState == DataRowState.Deleted) { isDelete = true; _drCurrentMaster.RejectChanges(); } drDetail.RejectChanges(); isDeleteDt = true; break; } if (sql != string.Empty) { foreach (SqlField sqlField in tmp) { string fieldName = sqlField.FieldName; paraNames.Add(fieldName); if (drDetail[fieldName].ToString() != string.Empty) { paraValues.Add(drDetail[fieldName]); } else { paraValues.Add(DBNull.Value); } paraTypes.Add(sqlField.DbType); } if (isDelete) { _drCurrentMaster.Delete(); } if (isDeleteDt) { drDetail.Delete(); } success = DbData.UpdateData(sql, paraNames.ToArray(), paraValues.ToArray(), paraTypes.ToArray()); if (success && updateIdentity) { string pk = _drTable["Pk"].ToString(); object o = DbData.GetValue("select @@identity"); if (o != null) { drDetail[pk] = o; } } if (!success) { break; } } else { success = true; } } return(success); }