2009-02-04 4 views
0

Access 데이터베이스에 새 데이터를 추가 한 후 ComboBox를 새로 고치는 것처럼 보이지 않습니다.ComboBox ChildForm에서 데이터를 추가 한 후 새로 고침

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 query 
       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

당신의 BindingSource에이 데이터 집합을 사용하고 있습니다 :

저는 여기에 새로운 데이터를 추가하는 데 사용하는 코드는? 그렇다면 DataSet을 통해 삽입 작업을 수행하고 바인딩 소스를 새로 고쳐야합니다. 이 방법을 사용하면 복제 된 삽입 로직도 피할 수 있습니다.

또는 데이터 세트를 새로 고칠 수도 있지만이 방법은 데이터 세트의 기능을 활용하지 않으며 많은 중복 코드가 생성됩니다.

0

저는 최근 VB.NET에서 같은 문제가있었습니다. 나는 datasoure를 Nothing으로 설정한다는 것을 알았고 (C#에서는 null), 객체 목록에 할당하여 문제를 해결했습니다.

attendanceDataFiles.DataSource = Nothing 
    attendanceDataFiles.DataSource = myFileList.files