none
Updating Textbox with sqlDataTable RRS feed

  • Question

  • I’m trying to bind a textbox’s text to a sql datatable. In the following code the

    GetSystems() function is call when the SearchID property changes and the SystemTypeTxtBx.Text is repopulated to the current record. The problem I’m having is saving the changes to SystemTypeTxtBx.Text back to the datatable. In the systemsAdapter the Update command equals null even after calling the systemsCommandBuilder.GetUpdateCommand();.

            private SqlDataAdapter systemsAdapter = new SqlDataAdapter();
            private DataTable systemsTable = new DataTable();
            private SqlCommandBuilder systemsCommandBuilder;
            private string SearchID;
            private string ConnStr = @"Data Source=OFFICE02;Initial Catalog=RapidServ;Integrated Security=True";
    
    
            private void GetSystems()
            {
                string SystemsQueryStr = @"SELECT [ID],[FkPSAgreementsID],[AreaSystemServes],[SystemType]
                                    FROM [RapidServ].[dbo].[PSASystems]
                                    WHERE FkPSAgreementsID = " + SearchID;
                SqlConnection systemsConn = new SqlConnection(ConnStr);
                SqlCommand command = new SqlCommand(SystemsQueryStr, systemsConn);
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(systemsAdapter);
                systemsAdapter.SelectCommand = command;
                systemsCommandBuilder = new SqlCommandBuilder(systemsAdapter);
                systemsTable.Clear();
                systemsAdapter.Fill(systemsTable);
                systemsCommandBuilder.GetUpdateCommand();
                if (SystemTypeTxtBx.DataBindings.Count < 1)
                {
                    SystemTypeTxtBx.DataBindings.Add(new Binding("Text", systemsTable, "SystemType"));
                }
            }
    
            private void SystemsSaveChangesBtn_Click(object sender, EventArgs e)
            {
                this.systemsAdapter.Update(systemsTable);
            }

    Thanks for your assistance,


    why767

    Monday, January 26, 2015 4:14 AM

Answers

  • I finally got it I have to use textboxes DataBindings.BindingManagerBase.EndCurrentEdit() function.
            private void SystemsSaveChangesBtn_Click(object sender, EventArgs e)
            {
                SystemTypeTxtBx.DataBindings["Text"].BindingManagerBase.EndCurrentEdit();
                this.systemsAdapter.Update(systemsTable);
            }
    
    Thanks for your input Cor.
     

    why767

    • Marked as answer by why767 Tuesday, January 27, 2015 3:50 AM
    Tuesday, January 27, 2015 3:50 AM

All replies

  • Why you add twice a commandbuilder to the systemsAdapter?

    I don't know if that causes the problem but I would avoid it.


    Success
    Cor

    Monday, January 26, 2015 12:21 PM
  • Oops, I remove the second SqlCommandBuilder commandBuilder = new SqlCommandBuilder(systemsAdapter);
    in the GetSystems() function but I still get the same results. I don't get any error but the changes are just not saved back to the table.


    why767

    Monday, January 26, 2015 1:57 PM
  • I don't know if this causes it, but you should remove the first.

    However, you should also be aware that you do a row change before the update or do an endedit on the bindingsource.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.endedit(v=vs.110).aspx


    Success
    Cor


    Monday, January 26, 2015 2:01 PM
  • Thanks I've looked at that example before and it doesn't update the data back to the table. I've modified my code to the following and still get the same.

          private void GetSystems()
            {
    
                string SystemsQueryStr = @"SELECT [ID],[FkPSAgreementsID],[AreaSystemServes],[SystemType]
                                    FROM [RapidServ].[dbo].[PSASystems]
                                    WHERE FkPSAgreementsID = " + SearchID;
                SqlConnection systemsConn = new SqlConnection(ConnStr);
                SqlCommand command = new SqlCommand(SystemsQueryStr, systemsConn);
                systemsAdapter.SelectCommand = command;
                SqlCommandBuilder systemsCommandBuilder = new SqlCommandBuilder(systemsAdapter);
                systemsTable.Clear();
                systemsAdapter.Fill(systemsTable);
                systemsCommandBuilder.GetUpdateCommand();
    
                if (SystemTypeTxtBx.DataBindings.Count < 1)
                {
                    SystemTypeTxtBx.DataBindings.Add(new Binding("Text", systemsTable, "SystemType"));
                }
            }
    
    
            private void SystemsSaveChangesBtn_Click(object sender, EventArgs e)
            {
                this.Validate();
                systemsBindingSource.EndEdit();
                this.systemsAdapter.Update(systemsTable);
            }


    why767

    Monday, January 26, 2015 3:08 PM
  • I finally got it I have to use textboxes DataBindings.BindingManagerBase.EndCurrentEdit() function.
            private void SystemsSaveChangesBtn_Click(object sender, EventArgs e)
            {
                SystemTypeTxtBx.DataBindings["Text"].BindingManagerBase.EndCurrentEdit();
                this.systemsAdapter.Update(systemsTable);
            }
    
    Thanks for your input Cor.
     

    why767

    • Marked as answer by why767 Tuesday, January 27, 2015 3:50 AM
    Tuesday, January 27, 2015 3:50 AM