Hi folks good afternoon,
First I would like to apologize myself, because I started this system in portuguese, the next one I will use only fields in english.
I'm having a trouble to get the last ID of a table and pass it to a variable, to show in a Textbox.
Could you please help-me with that?
LastID is declared at the top of the form.
Int32 LastID = 0;
Table: Base_Titulos_Pagos
Table's ID : Cod_Base_Titulos_Pagos
Here follows the code. I'm using SELECT MAX, but is returning"0". There are four (4) registers in this table.
If you guys could help-me with a Lambda expression also.
Thanks a lot for your help.
private void btn_InserirPagto_Click(object sender, EventArgs e) { Data_Agora = DateTime.Now; // data_LoginTextBox.Text = Data_Agora.ToShortDateString(); // login_NameTextBox.Text = Login.DadosGerais.Loginusuario; btn_salvar.Enabled = true; btn_salvar.BackColor = Color.DarkOrange; if (data_PagamentoDateTimePicker1.Text != " " && valor_PagoTextBox1.Text != " " && banco_DebitadoTextBox1.Text != " " && numero_ChequeTextBox1.Text != " ") { try { cmd = new SqlCommand("INSERT INTO Base_Titulos_Pagos(Data_Pagamento, Total_Pago, Banco_Debitado, Numero_Cheque, Historico, Data_Login, Login_Name)"+ "VALUES(@Data_Pagamento, @Total_Pago, @Banco_Debitado, @Numero_Cheque, @Historico, @Data_Login, @Login_Name)", conn); conn.Open(); DateTime DataPagamento = Convert.ToDateTime(data_PagamentoDateTimePicker1.Value.ToShortDateString()); cmd.Parameters.AddWithValue("@Data_Pagamento", DataPagamento); decimal TotalPago = Convert.ToDecimal(valor_PagoTextBox1.Text); cmd.Parameters.AddWithValue("@Total_Pago", TotalPago); Int32 BancoDebitado = Convert.ToInt32(banco_DebitadoTextBox1.Text); cmd.Parameters.AddWithValue("@Banco_Debitado", BancoDebitado); String NumeroCheque = Convert.ToString(numero_ChequeTextBox1.Text); cmd.Parameters.AddWithValue("@Numero_Cheque", NumeroCheque); String Historico = Convert.ToString(historicoTextBox1.Text); cmd.Parameters.AddWithValue("@Historico", Historico); DateTime Data_Agora = DateTime.Now; cmd.Parameters.AddWithValue("@Data_Login", Data_Agora); String LoginName = Login.DadosGerais.Loginusuario; cmd.Parameters.AddWithValue("@Login_Name", LoginName); Int32 result = cmd.ExecuteNonQuery(); MessageBox.Show("Inicie a Seleção de Títulos para Pagamentos! " + result.ToString() + "Cheque Inserido!"); cmd = new SqlCommand("SET @LastID = SELECT MAX (Cod_Base_Titulos_Pagos) FROM Base_Titulos_Pagos"); cod_Base_Titulos_PagosTextBox.Text = Convert.ToString(LastID); cod_Base_Titulos_PagosTextBox.Refresh(); numero_Parcelas_a_PagarDataGridView.Enabled = true; } catch (Exception Ex) { MessageBox.Show("Erro! " + Ex.Message); } finally { conn.Close(); btn_fechar.Enabled = true; } } else { MessageBox.Show("Informe todos os Dados necessários para incluir o Pagamento do Título!"); } }