2016-04-13 9 views
0

Kann ich wissen, warum bekomme ich seltsame Ausgabe, wobei die Combobox nicht aktualisiert und neue Werte anzeigen, wenn ein Wert aus einem anderen Formular zur Datenbank hinzugefügt wird. Im Folgenden meines BeispielcodeAktualisiere Wert in der Combobox von einem anderen Formular nach dem Einfügen in die Datenbank

Dies ist Form Code, wo die Combobox braucht

public partial class Form1 : Form 
{ 
    OleDbConnection con = new OleDbConnection("Provider=MSDAORA;Data Source=orcl;User ID=nareen1093;Password=nareen1093;Unicode=True"); 

    public Form1() 
    { 
     InitializeComponent(); 
     loadButton(); 
    } 

    public void loadButton() 
    { 
     con.Open(); 
     OleDbDataAdapter oda = new OleDbDataAdapter("select serial_number from fingerprint_device where serial_number like '%'", con); 
     DataTable dt = new DataTable(); 
     oda.Fill(dt); 
     comboBox1.DataSource = dt; 
     comboBox1.DisplayMember = "serial_number"; 
     comboBox1.SelectedIndex = -1; 
     con.Close(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     Form2 form2 = new Form2(); 
     form2.ShowDialog(); 
    } 
} 

dies form2 aktualisiert werden, wo der Einsatz in Datenbank stattfindet.

public partial class Form2 : Form 
{ 
    OleDbConnection con = new OleDbConnection("Provider=MSDAORA;Data Source=orcl;User ID=nareen1093;Password=nareen1093;Unicode=True"); 

    public Form2() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     con.Open(); 

     try 
     { 
      OleDbCommand cmd = new OleDbCommand("insert into fingerprint_device values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')", con); 
      cmd.ExecuteNonQuery(); 
      MessageBox.Show("Registration Success", "Status", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      con.Close(); 
      Form1 form1 = new Form1(); 
      form1.loadButton(); 

     } 

     catch (Exception ex) 
     { 
      MessageBox.Show(ex + "", "Status", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 

     finally 
     { 
      con.Close(); 
     } 
    } 
} 

Antwort

0

Sie tun ein paar Dinge, die das Problem verursachen.

Erstes Problem

In Ihrem Form2 klicken Sie NEW Form erstellen, rufen loadbutton diese INSTANZ und in der Nähe Form2. Daher sind alle Änderungen an Form2 jetzt weg. Dies geschieht, wenn die Ausführung von form2.ShowDialog(); beendet wird. Sie haben also Ihre ComboBox aktualisiert, aber die Aktualisierung erfolgte auf einer neuen Instanz von Form1, und sie wird ohne das Ergebnis angezeigt.

Zweites Problem

Sie sind das Hinzufügen neuer Werte DB auf Form2, aber Sie haben Ihre Combo in Form1 nicht aktualisiert. Da die neue in Form2 erstellte Instanz von Form1 entfernt wird (siehe Erstes Problem), wird das Steuerelement "comboBox" in der aktuellen Instanz von "Form1" nicht aktualisiert.

Lösung

entfernen Form1 form1 = new Form1(); und form1.loadButton(); von Form2 button1_Click. Fügen Sie jetzt loadButton() nach form2.ShowDialog();

Nun, wenn Form2 geschlossen ist, wird Ihre loadButton() führen und aktualisieren Combo.

public partial class Form1 : Form 
{ 
OleDbConnection con = new OleDbConnection("Provider=MSDAORA;Data Source=orcl;User ID=nareen1093;Password=nareen1093;Unicode=True"); 

public Form1() 
{ 
    InitializeComponent(); 
    loadButton(); 
} 

public void loadButton() 
{ 
    con.Open(); 
    OleDbDataAdapter oda = new OleDbDataAdapter("select serial_number from fingerprint_device where serial_number like '%'", con); 
    DataTable dt = new DataTable(); 
    oda.Fill(dt); 
    comboBox1.DataSource = dt; 
    comboBox1.DisplayMember = "serial_number"; 
    comboBox1.SelectedIndex = -1; 
    con.Close(); 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    Form2 form2 = new Form2(); 
    form2.ShowDialog(); 
    loadButton(); 
} 
} 

public partial class Form2 : Form 
{ 
OleDbConnection con = new OleDbConnection("Provider=MSDAORA;Data Source=orcl;User ID=nareen1093;Password=nareen1093;Unicode=True"); 

public Form2() 
{ 
    InitializeComponent(); 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    con.Open(); 

    try 
    { 
     OleDbCommand cmd = new OleDbCommand("insert into fingerprint_device values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')", con); 
     cmd.ExecuteNonQuery(); 
     MessageBox.Show("Registration Success", "Status", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     con.Close(); 

    } 

    catch (Exception ex) 
    { 
     MessageBox.Show(ex + "", "Status", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 

    finally 
    { 
     con.Close(); 
    } 
} 
} 
+0

Herr, vielen Dank für die Erklärung, und ich habe jetzt verstanden. Ich war jetzt nur zurück, das konnte ich nicht antworten – Programmer

Verwandte Themen