自动填充文本框的结果.文本从数据库,基于2个不同的组合框

本文关键字:文本 2个 基于 组合 填充 结果 数据库 | 更新日期: 2023-09-27 18:17:37

namespace Training
{
public partial class AddingNewData : Form
{
    public AddingNewData()
    {
        InitializeComponent();
        fillcombo1();
        fillcombo2();
        autopopulatedays();
}
string original_city, destination_city;
void fillcombo1()
{
    string constring = "datasource=localhost;port=3306;username=root;password=root";
    string Query = "SELECT * FROM itemdelivery.fee GROUP BY orig_city;";
    MySqlConnection conDataBase = new MySqlConnection(constring);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
    MySqlDataReader myReader;
    try
    {
        conDataBase.Open();
        myReader = cmdDataBase.ExecuteReader();
        while (myReader.Read())
        {
            string storig = myReader.GetString("orig_city");
            comboBox1.Items.Add(storig);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
void fillcombo2()
{
    string constring = "datasource=localhost;port=3306;username=root;password=root";
    string Query = "SELECT * FROM itemdelivery.fee GROUP BY dest_city;";
    MySqlConnection conDataBase = new MySqlConnection(constring);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
    MySqlDataReader myReader;
    try
    {
        conDataBase.Open();
        myReader = cmdDataBase.ExecuteReader();
        while (myReader.Read())
        {
            string stdest = myReader.GetString("dest_city");
            comboBox2.Items.Add(stdest);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    original_city = comboBox1.Text;
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    destination_city = comboBox2.Text;
}
private void txt_deliverytime_TextChanged(object sender, EventArgs e)
{
}
void autopopulatedays()
{
    string constring = "datasource=localhost;port=3306;username=root;password=root";
    string Query = "SELECT `del_time` FROM `itemdelivery.fee` WHERE `orig_city` = @oc AND `dest_city`= @dc";
    MySqlConnection conDataBase = new MySqlConnection(constring);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
    try
    {
        conDataBase.Open();
        cmdDataBase.Parameters.AddWithValue("@oc", original_city);
        cmdDataBase.Parameters.AddWithValue("@dc", destination_city);
        object result = cmdDataBase.ExecuteScalar();
        if (result != null)
            txt_deliverytime.Text = result.ToString();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
  }
}
}

所以,我有两个组合框,选择其中的两个后,我把original_citydestination_city的值作为string,使用comboBox1_SelectedIndexChangedcomboBox2_SelectedIndexChanged

然后在方法autopopulatedays()上,我试图通过使用数据库上的original_citydestination_city的值来匹配单个整数值来自动填充txt_deliverytime.Text查询。

但是,为什么我失败了呢?我得到的唯一错误是"No database selected",这对我来说很奇怪,当我选择2个组合框选项时txt_deliverytime.Text不会自动填充。

========================================================================

// UPDATE VERSION
// Connected to database, and ignoring the code before(above),
// This code only meant to auto-populate txt_deliverytime.Text when
// combobox1.selectitem and combobox2.selectitem
// Why it's still wrong?.

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        original_city = comboBox1.SelectedItem.ToString();
    }
    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        destination_city = comboBox2.SelectedItem.ToString();
    // if the combobox1 is not empty and combobox2 is also not empty, then run this code
        if (comboBox1.SelectedItem.ToString() != null)
        {
            string constring = "datasource=localhost;port=3306;username=root;password=root";
            string Query = "SELECT del_time FROM itemdelivery.fee WHERE orig_city='" + original_city + "' AND dest_city='" + destination_city + "';";
             // if I run this query on MySQL, it will show only a column name del_time with only a single row, 
             // thus only show a value, I want to get that value to txt_deliverytime.Text
             MySqlConnection conDataBase = new MySqlConnection(constring);
             MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
            conDataBase.Open();
            string getValue = cmdDataBase.ExecuteScalar().ToString();
            if (getValue != null)
            {
                txt_deliverytime.Text = getValue.ToString();
        // meant to change it here, but seems not successful
            }
            conDataBase.Close();
        }
    }

自动填充文本框的结果.文本从数据库,基于2个不同的组合框

首先使用有效的ConnectionString:

Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;

Pwd =我的密码;

那么我认为这不是你的实际代码,因为有一个少花括号

public partial class AddingNewData : Form
{
    public AddingNewData()
    {
        InitializeComponent();
        fillcombo1();
        fillcombo2();
        autopopulatedays();
}

最后,当comboBox1或comboBox2选择的项目发生变化时,您应该'自动填充天数',而不仅仅是当您填充组合时。

我将上面的代码分解成下面的代码=

namespace Training
{
    public partial class AddingNewData : Form
    {
        public AddingNewData()
        {
            InitializeComponent();
            fillcombo1();
            fillcombo2();
    }
    string original_city, destination_city;
void fillcombo1()
{
    string constring = "datasource=localhost;port=3306;username=root;password=root";
    string Query = "SELECT * FROM itemdelivery.fee GROUP BY orig_city;";
    MySqlConnection conDataBase = new MySqlConnection(constring);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
    MySqlDataReader myReader;
    try
    {
        conDataBase.Open();
        myReader = cmdDataBase.ExecuteReader();
        while (myReader.Read())
        {
            string storig = myReader.GetString("orig_city");
            comboBox1.Items.Add(storig);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
void fillcombo2()
{
    // EMPTY
}
// just some improvement on query
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    original_city = comboBox1.SelectedItem.ToString();
    string constring = "datasource=localhost;port=3306;username=root;password=root";
    string Query = "SELECT DISTINCT dest_city FROM itemdelivery.fee WHERE orig_city = '" + original_city + "' GROUP BY destination_city ;";
    MySqlConnection conDataBase = new MySqlConnection(constring);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
    MySqlDataReader myReader;
    try
    {
        conDataBase.Open();
        myReader = cmdDataBase.ExecuteReader();
        while (myReader.Read())
        {
            string stdest = myReader.GetString("dest_city");
            comboBox2.Items.Add(stdest);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
// this is where I solved the problem
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    destination_city = comboBox2.SelectedItem.ToString();
    string constring = "datasource=localhost;port=3306;username=root;password=root";
    string Query = "SELECT del_time FROM itemdelivery.fee WHERE orig_city ='" + original_city + "' AND dest_city ='" + destination_city + "';";
    MySqlConnection conDataBase = new MySqlConnection(constring);
    MySqlCommand cmdDataBase = new MySqlCommand(Query, conDataBase);
        try
        {
            conDataBase.Open();
            var result = cmdDataBase.ExecuteScalar();
                if (result != null)
                {
                    txt_deliverytime.Text = result.ToString();
                }
            conDataBase.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
}
private void txt_deliverytime_TextChanged(object sender, EventArgs e)
{
}

}