简体   繁体   English

插入数据库后,从另一种形式更新组合框中的值

[英]Updating value in combobox from another form after inserted to database

Can i know why am i getting weird output whereby the combobox does not refresh and show new values when a value is added to database from another form. 我能知道为什么我得到奇怪的输出,从而当从另一种形式向数据库添加值时,组合框不会刷新并显示新值的问题。 Below my sample code 在我的示例代码下面

This is form code where the combobox need to be updated 这是需要更新组合框的表单代码

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();
    }
}

this is form2 where the insert into database takes place. 这是在form2中插入数据库的位置。

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();
        }
    }
}

You are doing couple of things that cause the problem. 您正在做一些导致问题的事情。

First Problem 第一个问题

In your Form2 button click you create a NEW form, call loadButton of this INSTANCE and close Form2. 在您的Form2按钮中,单击创建一个表单,调用此INSTANCE的 loadButton并关闭Form2。 So, any changes made to Form2 is gone now. 因此,对Form2所做的任何更改现在都消失了。 This happens as execution of form2.ShowDialog(); 这是在执行form2.ShowDialog();时发生的form2.ShowDialog(); finishes. 完成。 So, you have updated your comboBox, but update happened on a new instance of Form1, and it is disposed without showing the result. 因此,您已经更新了comboBox,但是更新发生在Form1的新实例上,并且在不显示结果的情况下将其丢弃。

Second Problem 第二个问题

You are adding new values to DB on Form2, but you haven't updated your combo in Form1. 您正在向Form2上的DB添加新值,但尚未在Form1中更新组合。 Since new instance of Form1 created in Form2 is disposed(refer to First Problem), again, comboBox control in current instance of Form1 is not updated. 由于处理了在Form2中创建的Form1的新实例(请参阅第一个问题),因此,同样不会更新Form1当前实例中的comboBox控件。

Solution

Remove Form1 form1 = new Form1(); 删除Form1 form1 = new Form1(); and form1.loadButton(); form1.loadButton(); from Form2 button1_Click . 从Form2 button1_Click Now add loadButton() after form2.ShowDialog(); 现在在loadButton()之后添加loadButton() form2.ShowDialog();

Now, when Form2 is closed your loadButton() will execute and update combo. 现在,当Form2关闭时,您的loadButton()将执行并更新组合。

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();
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM