I have been working on a form that reads records from a SQL database. I have the binding working in that it populates all of the fields with the correct data. But if I am viewing record 1 and record 2 changes while I'm looking at 1, then browse to record 2, it shows me the old data unless I close and re-open the form which causes the binding to happen over again with the new data.

Is there a way to have the data in the dataset updated with each record change?

Here is the relevant form code...any suggestions would be greatly appreciated:

public partial class EditorAccounts : Form
    {
        #region Globals

        BindingSource bindsrc;

        #endregion

        public EditorAccounts()
        {
            InitializeComponent();
        }

        #region On Load Events

        private void EditorAccounts_Load(object sender, EventArgs e)
        {

            #region Error Handlers

            Messages errors = new Messages();

            #endregion

            #region Connect to Database

            //get default server type
            string ServerType = Properties.Settings.Default.SQLType.ToString();

            //get our connection string
            ConnectionManager ConnectionString = new ConnectionManager();
            string SQLConnectionString = ConnectionString.ConnectionString(ServerType);
            SqlConnection Connection = new SqlConnection();
            Connection.ConnectionString = SQLConnectionString;

            //try opening the connection
            try
            {
                Connection.Open();
                Connection.Close();
            }
            catch
            {
                //connection failed
                //give error message
                errors.ErrorMessages(004);
            }

            #endregion

            #region Retrieve Data

            try
            {
                SqlCommand query = new SqlCommand("SELECT * FROM accounts", Connection);
                SqlDataAdapter adrQuery = new SqlDataAdapter(query);
                DataSet dsQuery = new DataSet();
                adrQuery.Fill(dsQuery, "accounts");

                bindsrc = new BindingSource(dsQuery, "accounts");

                DataNavigator.BindingSource = bindsrc;

                #region Bindings

                //bind our data to the fields
                //I cut this out of my post because it was forever and a day long.

                #endregion

            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }

            #endregion

        }

I think I figured out what I need to do, but....I'm not quite sure how to do it?

I think i need to call adrQuery.Fill(dsQuery, "accounts") again, but since that dataset that needs to be filled again is located in the on_load portion of the form, I can't call it again without recreating the entire data adapter etc... and that just seems like a bad idea?

Any suggestions?

I figured it out.

I placed:
SqlCommand query;
SqlDataAdapter adrQuery;
DataSet dsQuery;
in globals and then just called:

int recordCurrent = int.Parse(DataNavigatorPositionItem.Text) - 1;
            dsQuery.Clear();
            adrQuery.Fill(dsQuery, "accounts");
            bindsrc.Position = recordCurrent;

in any event that I need the recordset to update and it works!

Just in case anyone else had the same problem :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.