Using: c #, windows forms, postgresql
I'm having a hard time putting together a routine which writes data to a table and retrieves the id (serial / sequence) and updates the log in another table.
Worst of all is that everything works by Debugging, but if direct run of the exception:
ERROR: 55000: The current value of the string "database_id_seq" has not been defined in this session.
This error happens when going through the routine "Search_ID".
I do not know what might be happening, or how to get around this, for how can one thing work by debugging and running does not work?
The following are some of the codes below:
Save
protected override void Gravar_Registro()
{
using (base.cn = Dados.getInstancia().getConexao())
{
cn.Open();
NpgsqlTransaction tr = cn.BeginTransaction(IsolationLevel.ReadCommitted);
try
{
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = DAL.Banco.Insert();
Tela_Para_Banco(cmd);
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
Log_Sistema(tr, Rotina.Usuario_Login, this.Text, cmd);
tr.Commit();
cn.Close();
}
catch (Exception ex)
{
tr.Rollback();
throw new Exception("Servidor SQL Erro: " + ex.Message);
}
}
beCodigo.IdTable = Buscar_ID("banco").ToString();
beCodigo.Text = beCodigo.IdTable;
}
LOG
private void Log_Sistema(NpgsqlTransaction tr, string strUsuario, string strTela, NpgsqlCommand cCommand)
{
using (NpgsqlConnection cn = Dados.getInstancia().getConexao())
{
try
{
cn.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = cn;
cmd.CommandText = "insert into log (data, usuario, tela, sql) values (current_timestamp, @usuario, @tela, @sql);";
cmd.Parameters.AddWithValue("@usuario", strUsuario);
cmd.Parameters.AddWithValue("@tela", strTela);
string tTexto = "";
for (int i = 0; i < cCommand.CommandText.Length - 1; i++)
{
tTexto = tTexto + cCommand.CommandText.Substring(i, 1);
if ((cCommand.CommandText.Substring(i, 1) == ",") || (cCommand.CommandText.Substring(i, 1) == " ") || (cCommand.CommandText.Substring(i, 1) == ")"))
{
foreach (NpgsqlParameter p in cCommand.Parameters)
{
string sParametro_Nome = p.ParameterName.ToString();
string sParametro_Valor = Rotina.ReturnEmptyIfNull(p.Value);
if (tTexto.IndexOf(sParametro_Nome) > 0)
if (tTexto.Substring(tTexto.IndexOf(sParametro_Nome), (tTexto.Length - 1) - tTexto.IndexOf(sParametro_Nome)) == sParametro_Nome)
{
tTexto = tTexto.Replace(sParametro_Nome, sParametro_Valor);
break;
}
}
}
}
if (tTexto != "")
cmd.Parameters.AddWithValue("@sql", tTexto);
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception("Servidor SQL Erro: " + ex.Message);
}
finally
{
cn.Close();
}
}
}
Retrieve ID
public static string Buscar_ID(string sTabela)
{
using (NpgsqlConnection cn = Dados.getInstancia().getConexao())
{
try
{
cn.Open();
NpgsqlCommand cmd = new NpgsqlCommand((String.Format("select currval('public.{0}_id_seq');", sTabela)), cn);
NpgsqlDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
if (dr.IsDBNull(0))
return "1";
else
return dr[0].ToString();
else
return "1";
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
cn.Close();
}
}
}