none
Update code is not working - Either way

    Question

  • Hi folks, I need a help from you.

    I'm trying to do an Update or a record, and is not working either way,  with command Update, using table adapter, and

    the other way using the parameters. Can anyone, for god sake tell me how to proceed to have the Update done!

    I'm not getting any error message. The DataGridView populates, I've tried to send the data from the dataGridview to the textboxes, and it does not work. The sequence of the parameters is the same from the database.

    Below follows the code:

        private void Entrada_Titulos_a_Receber_Load(object sender, EventArgs e)
            {
    
    
                // TODO: This line of code loads data into the 'entradaTitulosaReceberDGV.Entrada_Titulos_a_Receber' table. You can move, or remove it, as needed.
                   this.entrada_Titulos_a_ReceberTableAdapter.Fill(this.entradaTitulosaReceberDGV.Entrada_Titulos_a_Receber);
                // TODO: This line of code loads data into the 'numeroParcelasaReceberDGV.Numero_Parcelas_a_Receber' table. You can move, or remove it, as needed.
                  this.numero_Parcelas_a_ReceberTableAdapter.Fill(this.numeroParcelasaReceberDGV.Numero_Parcelas_a_Receber); 
                  this.clientes_Contas_a_ReceberBindingSource.DataSource = DataContextFactory.DataContext.Clientes_Contas_a_Recebers;
                  this.numero_Parcelas_a_ReceberBindingSource.DataSource = DataContextFactory.DataContext.Numero_Parcelas_a_Recebers;
                  this.status_Titulos_a_ReceberBindingSource.DataSource = DataContextFactory.DataContext.Status_Titulos_a_Recebers;
                  this.tipos_ReceitaBindingSource.DataSource = DataContextFactory.DataContext.Tipos_Receitas;
                  TryRefreshData();
                  TryRefreshTipoReceita();
                  TryRefreshStatusTitulo();
                string constr = @"Data Source=DESKTOP-3O98051;Initial Catalog=SGFRenaissance;Integrated Security=True";
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    SqlDataAdapter sda = new SqlDataAdapter("Select * From Entrada_Titulos_a_Receber", conn);
                    DataSet Ds = new DataSet();
                    sda.Fill(Ds, "T1");
                    entrada_Titulos_a_ReceberDataGridView.DataSource = Ds.Tables["T1"];
                 
                }
                // Bind the "SelectionChanged" here
                entrada_Titulos_a_ReceberDataGridView.SelectionChanged += entrada_Titulos_a_ReceberDataGridView_SelectionChanged;
                GetDetails();
    
            }
    
            void GetDetails()
            {
                string constr = @"Data Source=DESKTOP-3O98051;Initial Catalog=SGFRenaissance;Integrated Security=True";
                int currentId;
                int index = entrada_Titulos_a_ReceberDataGridView.CurrentCell.RowIndex;
                currentId = Convert.ToInt32(entrada_Titulos_a_ReceberDataGridView.Rows[index].Cells["Cod_Id"].Value);
                using (SqlConnection conn = new SqlConnection(constr))
                {
                    var select = "Select * From Numero_Parcelas_a_Receber Where Cod_Entrada_Titulos_a_Receber =" + currentId;
                    SqlDataAdapter sda = new SqlDataAdapter(select, conn);
                    DataSet Ds = new DataSet();
                    sda.Fill(Ds, "T2");
                    numero_Parcelas_a_ReceberDataGridView.DataSource = Ds.Tables["T2"];
                }
            }
    
            private void btn_Salvar_Click(object sender, EventArgs e)
            {
               
                    Data_Agora = DateTime.Now;
                    Data_Agora.Date.ToString("ddMMyyyy", System.Globalization.CultureInfo.InvariantCulture);
                    data_logintxtbox.Text = DateTime.Now.ToString();
                    data_logintxtbox.Refresh();
                    login_NameTextBox.Text = Login.DadosGerais.Loginusuario;
                    data_logintxtbox.Text = Data_Agora.ToShortDateString();
              //    this.entrada_Titulos_a_ReceberTableAdapter.Update(this.entradaTitulosaReceberDGV.Entrada_Titulos_a_Receber); // did not work either.
            //      MessageBox.Show("Dados do Título Atualizados com Sucesso!");
                    ExibirDadosTitulo();
    
            
                    string constr = @"Data Source=DESKTOP-3O98051;Initial Catalog=SGFRenaissance;Integrated Security=True";
                    using (SqlConnection conn = new SqlConnection(constr))
    
                        if (cod_Entrada_Titulos_a_ReceberTextBox.Text != "" && Cod_ReceitaTextBox.Text != "" && Cod_StatusTextBox.Text != "" && numero_ParcelasTextBox.Text != "" && data_VencimentoDateTimePicker.Text != "" && dateTimePicker_NF.Text != "" && numero_NFTextBox.Text != "" && valor_Pago_a_VistaTextBox.Text != "" && valor_NFTextBox.Text != "" && valor_Pago_a_VistaTextBox.Text != "" && textBox_CV.Text !="")
                        {
                            try
                            {
                                cmd = new SqlCommand("UPDATE Entrada_Titulos_a_Receber SET Cod_Cliente=@Cod_Cliente, Numero_NF=@Numero_NF, Data_NF=@Data_NF, Valor_NF=@Valor_NF, Cod_Receita=@Cod_Receita, Data_Vencimento=@Data_Vencimento, Numero_Parcelas=@Numero_Parcelas, Valor_Pago_a_Vista=@Valor_Pago_a_Vista, Cod_Status_Titulo=@Cod_Status_Titulo, Codigo_CV=@Codigo_CV, Historico=@Historico, Data_Login=@Data_Login, Login_Name=@Login_Name  WHERE (Cod_Entrada_Titulos_a_Receber=@Cod_Entrada_Titulos_a_Receber)", conn);
                                conn.Open();
                                cmd.Parameters.AddWithValue("@Cod_Entrada_Titulos_a_Receber", ID);
                                Int32 Cod_Cliente = Int32.Parse(cod_ClienteTextBox.Text);
                                cmd.Parameters.AddWithValue("@Cod_Cliente", Cod_Cliente);
                                cmd.Parameters.AddWithValue("@Numero_NF", numero_NFTextBox.Text);
                                cmd.Parameters.AddWithValue("@Data_NF", dateTimePicker_NF.Value);
                                decimal valor_NF = decimal.Parse(valor_NFTextBox.Text);
                                cmd.Parameters.AddWithValue("@Valor_NF", valor_NF);
                                Int32 Cod_Receita = Int32.Parse(Cod_ReceitaTextBox.Text);
                                cmd.Parameters.AddWithValue("@Cod_Receita", Cod_Receita);
                                cmd.Parameters.AddWithValue("@Data_Vencimento", data_VencimentoDateTimePicker.Value);     
                                Int32 Numero_Parcelas = Int32.Parse(numero_ParcelasTextBox.Text);
                                cmd.Parameters.AddWithValue("@Numero_Parcelas", Numero_Parcelas);
                                decimal valor_Pago_a_Vista = decimal.Parse(valor_Pago_a_VistaTextBox.Text);
                                cmd.Parameters.AddWithValue("@Valor_Pago_a_Vista", valor_Pago_a_Vista);
                                Int32 Cod_StatusTitulo = Int32.Parse(Cod_StatusTextBox.Text);
                                cmd.Parameters.AddWithValue("@Cod_Status_Titulo", Cod_StatusTitulo);
                                Int32 Cod_CV = Int32.Parse(textBox_CV.Text);
                                cmd.Parameters.AddWithValue("@Codigo_CV", Cod_CV);
                                cmd.Parameters.AddWithValue("@Historico", historicoTextBox.Text);
                                Data_Agora = Convert.ToDateTime(data_logintxtbox.Text);
                                cmd.Parameters.AddWithValue("@Data_Login", Data_Agora);
                                cmd.Parameters.AddWithValue("@Login_Name", login_NameTextBox.Text);
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Show! Dados do Título Atualizados com Sucesso!");
                                conn.Close();
                            }
                            catch (Exception Ex)
                            {
                                MessageBox.Show("Erro! Falha ao Tentar salvar o Registro! " + Ex.Message);
                            }
                            finally
                            {
                                conn.Close();
                                ExibirDadosTitulo();
                                ExibirDadosParcela();
                        //      LimparDados();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Informe todos os Dados Necessários para Atualizar o Título!");
                        }
                   
               
                     int currentIndex;
                     int flag = -1;
            }

    Thanks and best regards,



    Wednesday, May 22, 2019 3:35 AM

All replies

  • Hi F.oliveirarocha,

    Thank you for posting here.

    Based on your description, you want to update a record to database.

    You could try to add the following code after inserting data to database.

     private void load() 
            {
                this.dataGridView1.DataSource = null;
                using (SqlConnection conn = new SqlConnection(con))
                {
                    SqlDataAdapter sda = new SqlDataAdapter("Select * From Student", conn);
                    DataSet Ds = new DataSet();
                    sda.Fill(Ds, "T1");
                    dataGridView1.DataSource = Ds.Tables["T1"];
    
                }
            }
       MessageBox.Show("Show! Dados do Título Atualizados com Sucesso!");
       conn.Close();
       load();//call this method here.
    

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 22, 2019 6:02 AM
  • OK Jack,

    Thanks for your help. But the Update still doesn't work.

    Best Regards,

    Friday, May 24, 2019 3:24 AM
  • Hi F.oliveirarocha,

    Thanks for the feedback.

    Because of extensive database fields, I could not test your code. If you want to solve your problem further, you could provide the full code and database design with me via Onedrive.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 24, 2019 6:56 AM
  • Sure, why not?

    Just send me your orientation. 

    I use the google drive.  I've never used the onedrive.

    I will send all the details and also the database script. So then you can figure out what is going on.

    I appreciante you help. Thank you very much.

    Tell me what you need. Then I will send it to you.

    Best Regards,

    Saturday, May 25, 2019 5:26 PM
  • Hi Jack,

    I can put all the system on onedrive. No problem.

    I'll have to give you some information to access the right Forms, to fix the Update.

    Also I will make some comments for you and I will change the button to Update, so then its is going to be esier to find the code and the forms.

    I will do it tomorrow morning (monday). Let me now the information to send these files to you.

    Best regads,

    Monday, May 27, 2019 2:42 AM
  • Jack, how are you?

    Are going to continue this post ?

    Let me know what is your intention.

    Thanks and best regards,

    Friday, May 31, 2019 1:10 PM
  • Hi

    Thanks for the feedback.

    I am waiting for your information including the code and database design.

    >>I will send all the details and also the database script.

    However, I don't receive the information now.

    I hope that you could give me the information.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 3, 2019 1:22 AM