I'm having a problem converting an information that is coming from my database to a variable of type Int32
.
When I do the select max(cur_id) from tbl_curriculo;
I send the id information straight to a Dataset
which I call ds
.
I have my persistence return this ds
and turn it into int
so I can send this id
to another table that will receive that id
as foreign key, however I do not know how to do the conversion correctly .
Convert.toInt32()
the IDE accuses that everything is ok, but at the time I send the code I get the following error
Invalid Cast Exception was unhandled by user code An exception of type 'System.InvalidCastException' occurred in mscorlib.dll but was not handled in user code Additional information: Can not convert an object of type 'System.Data.DataSet' to type 'System.IConvertible'.
I know the conversion is not working that way, and I also tried to pass the dataset without doing the conversion to see if it worked and it did not work either.
Below is my code for the page where the conversion is being done
protected void btn_Confirmar_Click(object sender, EventArgs e)
{
CurriculoDB.InsertCurriculo(Persistencia.Cu[0]);
int id = Convert.ToInt32(CurriculoDB.SelectCur_Id());
CandidatoDB.InsertCandidato(Persistencia.Cand[0], Persistencia.En[0], id);
Persistencia.Cand.Clear();
Persistencia.Curf.Clear();
Persistencia.Cur.Clear();
Persistencia.Expt.Clear();
Persistencia.Contato.Clear();
Response.Redirect("ConfirmacaoCandidato.aspx");
}
Below the code for select
public static DataSet SelectCur_Id()
{
DataSet ds = new DataSet();
IDbConnection objConnection;
IDbCommand objCommand;
IDataAdapter objDataAdapter;
objConnection = Mapped.Connection();
objCommand = Mapped.Command("select max(cur_id) from tbl_curriculo", objConnection);
objDataAdapter = Mapped.Adapter(objCommand);
objDataAdapter.Fill(ds); // O objeto DataAdapter vai preencher o
// DataSet com os dados do BD, O método Fill é o responsável por preencher o DataSet
objConnection.Close();
objCommand.Dispose();
objConnection.Dispose();
return ds;
}
Below the code of the table where the insert
will be done
public static int InsertCandidato(Candidato can, Endereco end, int id)
{
int retorno = 0;
try
{
IDbConnection conexao;
IDbCommand comando;
string sql = "insert into tbl_candidato values(0, ?can_nome, ?can_data_nascimento, ?can_sexo, ?can_cpf, ?can_cep, ?can_rua, ?can_bairro, ?can_cidade, ?can_estado, ?can_numero, ?can_complemento, ?fk_tbl_candidato_cur_id);";
conexao = Mapped.Connection();
comando = Mapped.Command(sql, conexao);
comando.Parameters.Add(Mapped.Parameter("?can_nome", can.Nome_candidato));
comando.Parameters.Add(Mapped.Parameter("?can_data_nascimento", can.Data_nascimento));
comando.Parameters.Add(Mapped.Parameter("?can_sexo", can.Sexo));
comando.Parameters.Add(Mapped.Parameter("?can_cpf", can.Cpf));
comando.Parameters.Add(Mapped.Parameter("?can_cep", end.Cep));
comando.Parameters.Add(Mapped.Parameter("?can_rua", end.Rua));
comando.Parameters.Add(Mapped.Parameter("?can_bairro", end.Bairro));
comando.Parameters.Add(Mapped.Parameter("?can_cidade", end.Cidade));
comando.Parameters.Add(Mapped.Parameter("?can_estado", end.Estado));
comando.Parameters.Add(Mapped.Parameter("?can_numero", end.Numero));
comando.Parameters.Add(Mapped.Parameter("?can_complemento", end.Complemento));
comando.Parameters.Add(Mapped.Parameter("?fk_tbl_candidato_cur_id", id));
comando.ExecuteNonQuery();
conexao.Close();
comando.Dispose();
conexao.Dispose();
}
catch (Exception)
{
retorno = -2;
}
return retorno;
}