views:

1829

answers:

3

I can't seem to get my ComboBox to refresh after i add new data to the Access Database.

Here's the Code i use to add the new data:

private void btnAddUser_Click(object sender, EventArgs e)
    {
        AccountForm actFrm = new AccountForm();
        if (actFrm.ShowDialog() == DialogResult.OK)
        {
            try
            {
                this.oleDbDataAdapter1.InsertCommand.CommandText =
                    "INSERT INTO userTable (AccountName, Username, PopServer, PopPort, Psswrd, SmtpServer, SmtpPort, Email)" +
                    "VALUES     ('" + actFrm.txtAccName.Text + "','" + actFrm.txtUsername.Text + "','" + actFrm.txtPop3.Text + "','" + actFrm.txtPop3Port.Text + "','" + actFrm.txtPassword.Text + "','" + actFrm.txtSmtp.Text + "','" + actFrm.txtSmtpPort.Text + "','" + actFrm.txtEmail.Text + "')";

                //open the bridge between the application and the datasource
                this.oleDbConnection1.Open();

                this.oleDbDataAdapter1.InsertCommand.Connection = oleDbConnection1;

                //execute the qurey 
                this.oleDbDataAdapter1.InsertCommand.ExecuteNonQuery();

                //close the connection
                this.oleDbConnection1.Close();

                MessageBox.Show("User Added Successfully");  //inform the user
                //tried here to refresh and even open close the myConn connection. to no avail
            }
            catch (System.Data.OleDb.OleDbException exp)
            {
                //close the connection
                this.oleDbConnection1.Close();

                MessageBox.Show(exp.ToString());
            }
+2  A: 

Is your BindingSource using a DataSet? If so, you need to do the insert through the DataSet and, maybe, refresh your binding source. If you do it this way, you'll avoid the duplicated insert logic as well.

Alternatively, you can just refresh the DataSet but this method doesn't take advantage of the DataSet's powers and will lead to a lot duplicated code.

Austin Salonen
A: 

this is my code to load the data into the ComboBox

private void fnGetConnectedToDatabase()
    {
        string conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=..\\..\\server.mdb";
        try
        {
            //open the connection to the database
            myConn = new OleDbConnection(conStr);
            myConn.Open();
        }
        catch (OleDbException ex)
        {
            MessageBox.Show("Error in connection ..." + ex.Message);
        }

        string sqlStr = "SELECT * FROM userTable;";

        //Instantiate a DataAdapter by passing the sqlStr and Connection.
        //now data is in raw form
        dAdapter = new OleDbDataAdapter(sqlStr, myConn);

        //Instantiate a DataSet
        dset = new DataSet();

        //Gets a collection that provides the master mapping
        // between a source table and a DataTable
        dAdapter.TableMappings.Add("Table", "userTable");

        /*A data adapter object utilizes the Fill method 
        to populate a DataSet or a DataTable object with data 
        retrieved by a SELECT command. */
        dAdapter.Fill(dset);
        //When binding a DataSet, .NET automatically uses the corresponding
        //DataViewManager provided through the DataSet.DefaultViewManager property  
        this.dviewmanager = dset.DefaultViewManager;

        this.comboBox1.DataSource = this.dviewmanager;
        //display "studentTable.StudentID" in the ComboBox
        this.comboBox1.DisplayMember = "userTable.AccountName";

        //DataBinding for the TextBox controls
        this.PopServer.DataBindings.Add("Text", this.dviewmanager, "userTable.PopServer");
        this.PopPort.DataBindings.Add("Text", this.dviewmanager, "userTable.PopPort");

        // Close the connection to the database.
        this.myConn.Close();
    }

I did try calling this method again after adding the data and it works but i received an error of duplicate code. For this though the combobox is refresh but the coding to bind the new/old data information to the textboxes were not working anymore.

Any help with the codes would be most helpful as i am new to this

Did you write this all yourself? If so, step back and do research into ".net data binding tutorial" or "WinForms data binding" because you are making this awfully complicated.
Austin Salonen
A: 

I recently had the same problem in VB.NET. I found that setting the datasoure to Nothing (null in C# I believe) and then assigning it to my list of objects solved the problem.

i.e.

  attendanceDataFiles.DataSource = Nothing
  attendanceDataFiles.DataSource = myFileList.files
John C. Lieurance MS