开发者

How to display information from MySQL in a textbox?

开发者 https://www.devze.com 2023-04-09 03:02 出处:网络
I an new in creating programs in Visual Studio C#. I want to create a program that reads the information from MySQL and display the selected information in a textbox. What I have here is a code for di

I an new in creating programs in Visual Studio C#. I want to create a program that reads the information from MySQL and display the selected information in a textbox. What I have here is a code for displaying the information in a combobox.

This is the code to display information from MySQL in a combobox:

private void comboBoxDistrict_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboBoxDistrictName.Items.Clear();
        string query = "SELECT employee_lastname, employee_firstname, employee_mid开发者_如何学编程dlename FROM employee WHERE employee_district = '" + comboBoxDistrict.Text.ToString() + "'";
        DBConn db = new DBConn();
        DataTable tbl = db.retrieveRecord(query);
        foreach (DataRow row in tbl.Rows)
        comboBoxDistrictName.Items.Add(row[0].ToString() + ", " + row[1].ToString() + " " + row[2].ToString());
    }

What I need is to display the information in a textbox. Please help. Thanks.


Just Change the assign from your ComboBox to be your textBox

txtBox1.Text = row[0].ToString() + ", " + row[1].ToString() + " " + row[2].ToString());

You may take care of iteration, since just last row in this case will be shown in the textBox


Use StringBuilder for performance reasons and to get around the "problem" of just the last row showing up in your TextBox as in Rami's example:

var sb = new StringBuilder();

foreach (DataRow row in tbl.Rows)
{
    sb.AppendLine(string.Format("{0}, {1} {2}",
                                row[0].ToString(),
                                row[1].ToString(),
                                row[2].ToString()));
}

txtBox1.Text = sb.ToString();

or simply append the next row to the TextBox using "+=":

txtBox1.Text += row[0].ToString() + ", " +
                row[1].ToString() + " " +
                row[2].ToString()) + "\r\n";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号