c#基于两个不同的组合框,自动填充数据库中textboxt.Text的结果

c#基于两个不同的组合框,自动填充数据库中textboxt.Text的结果,c#,mysql,sql,visual-studio,C#,Mysql,Sql,Visual Studio,因此,我有两个组合框,在选择其中两个后,我使用comboBox1\u SelectedIndexChanged和Combox2\u SelectedIndexChanged将original_city和destination_city中的值作为string 然后在方法autopopulatedays()中,我尝试通过匹配数据库上的original\u city和destination\u city的值,使用单个整数值自动填充txt\u deliverytime.Text 这个问题 但是,为什么我

因此,我有两个组合框,在选择其中两个后,我使用
comboBox1\u SelectedIndexChanged
Combox2\u SelectedIndexChanged
original_city
destination_city
中的值作为
string

然后在方法
autopopulatedays()
中,我尝试通过匹配数据库上的
original\u city
destination\u city
的值,使用单个整数值自动填充
txt\u deliverytime.Text
这个问题

但是,为什么我失败了?我得到的唯一错误是
“未选择任何数据库”
,这对我来说很奇怪,当我选择两个组合框选项时
txt\u deliverytime.Text
不会自动填充

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

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

首先使用有效的
连接字符串

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

        }
    }
Pwd=我的密码

那么我想这不是您的实际代码,因为缺少一个花括号

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

最后,当comboBox1或comboBox2所选项目发生更改时,而不仅仅是当您填写组合时,您应该“autopopulatedays”。

我将上面的代码解析为下面这样的代码=

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

}

您需要在连接字符串或查询中提供数据库名称如果我删除autopopulatedays方法,我将不会出现“未选择数据库”错误。那么@viveknuna,你能告诉我怎么做你的建议吗?(又名代码)我只是更新了我的帖子,连接再也没有问题了,很好。请帮助我更正更新版本上的代码。您是否调试了
comboBox2\u SelectedIndexChanged(…)
?是否有
original\u city
destination\u city
的正确值?仍然无法在您的连接字符串中看到
Database=sth
我只是更新了我的帖子,连接不再有问题,没问题。请帮助我更正更新版本上的代码。
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)
{
}