public void salvarGrid() { if (gDt == null) { Alert("Sessão expirada!"); return; } gDt.TableName = G_Table; atualizaTabela(this.gDt); try { gDt.PrimaryKey = new DataColumn[] { gDt.Columns["id"] }; } catch { } for (int i = 0; i < gDt.Rows.Count; i++) { DataRow dr = gDt.Rows[i]; GridViewRow grw = gvwDados.Rows[i]; //if (!this.campoPreenchido(Utilities.Format.localizaControl("txtEmail", grw), "Informe Email!")) //{ // return false; //} int pk = Convert.ToInt32(Nvl(dr["id"], 0)); int max = ConnAccess.getMax(ConnAccess.getConn(), "id", G_Table, ""); if (pk <= 0) { ConnAccess.Insert(ConnAccess.getConn(), dr, "id", true); dr["id"] = ConnAccess.getMax(ConnAccess.getConn(), "id", G_Table, ""); } else { ConnAccess.Update(ConnAccess.getConn(), dr, "id"); } } this.my_dvMensagem.InnerHtml = "Dados salvos com sucesso!"; //Alert("Dados salvos com sucesso!"); }
/// <summary> /// Método para salvar os emails que vem da tela. /// </summary> /// <param name="id_contato"></param> /// <param name="oConn"></param> private void salvaEmails(int id_contato, IDbPersist oConn) { DataRow dr_email = ConnAccess.getNewRow(oConn, "contato_email"); int qtde_emails = RendLibrary.Helper.RequestValue <int>("qtde_emails").Value; string str_ids = "0"; int ordem = 0; string emails = ""; for (int i = 0; i <= qtde_emails; i++) { string email = RendLibrary.Helper.RequestString("email" + i.ToString()); if (email.Trim() != String.Empty) { ordem++; if (emails != String.Empty) { emails += ", "; } emails += email.Trim(); dr_email["email"] = email; dr_email["id_contato"] = id_contato; dr_email["ordem_cadastro"] = ordem; string sql_item = " select id from contato_email where email='" + email + "' and id_contato=" + id_contato.ToString(); object id_tmp = ConnAccess.executeScalar(oConn, sql_item); if (id_tmp != null && id_tmp != DBNull.Value) { dr_email["id"] = id_tmp; ConnAccess.Update(oConn, dr_email, "id"); } else { ConnAccess.Insert(oConn, dr_email, "id", true); dr_email["id"] = ConnAccess.getMax(oConn, "id", dr_email.Table.TableName, " where id_contato = " + id_contato.ToString()); } str_ids += "," + dr_email["id"].ToString(); } } ConnAccess.executeCommand(oConn, " delete from contato_email where id_contato = " + id_contato.ToString() + " and id not in ( " + str_ids + " ) "); ConnAccess.executeCommand(oConn, " update contato set emails = '" + emails + "' where id = " + id_contato.ToString()); }
public ActionResult Cadastro() { DataAccess.IDbPersist oConn = base.getMainConn(); DataRow registro = ConnAccess.getNewRow(oConn, "contato"); DataRow dr_email = ConnAccess.getNewRow(oConn, "contato_email"); DataTable dtEmails = dr_email.Table; string id = RendLibrary.Helper.RequestString("id"); string acao = RendLibrary.Helper.RequestString("acao"); string ispostback = RendLibrary.Helper.RequestString("ispostback"); if (ispostback == "1") { if (id.Trim() != String.Empty && RendLibrary.Helper.isInteger(id)) { registro = ConnAccess.getRow(oConn, "contato", "id", id); } RendLibrary.Helper.loadDataRowFromForm(registro); if (registro["data_cadastro"] == DBNull.Value) { registro["data_cadastro"] = DateTime.Now; } } if (acao == "SAVE") { if (registro["id"] == DBNull.Value) { registro["data_cadastro"] = DateTime.Now; ConnAccess.Insert(oConn, registro, "id", true); registro["id"] = ConnAccess.getMax(oConn, "id", "contato", ""); } else { ConnAccess.Update(oConn, registro, "id"); } if (registro["id"] != DBNull.Value) { this.salvaEmails(Convert.ToInt32(registro["id"]), oConn); } TempData["st_Mensagem"] = "Contato salvo com sucesso!"; //TempData["st_Mensagem"] = "Contato salvo com sucesso!"; Response.Redirect("Cadastro?id=" + registro["id"].ToString() + "&acao=LOAD", true); Response.End(); } if (acao == "DEL" && id.Trim() != String.Empty && RendLibrary.Helper.isInteger(id)) { ConnAccess.executeCommand(oConn, " delete from contato where id = " + id.ToString()); TempData["st_Mensagem"] = "Contato removido com sucesso!"; Response.Redirect("Cadastro", true); Response.End(); } if (id.Trim() != String.Empty && RendLibrary.Helper.isInteger(id)) { registro = ConnAccess.getRow(oConn, "contato", "id", id); dtEmails = ConnAccess.fetchData(oConn, " select * from contato_email where id_contato = " + id + " order by ordem_cadastro "); } if (dtEmails.Rows.Count <= 0) { dtEmails.Rows.Add(dr_email.ItemArray); } ViewData["registro"] = registro; ViewData["DtEmails"] = dtEmails; return(View()); }