简体   繁体   中英

How to add values to listbox from another listbox using SQL query?

User clicks a btnAdd and it transfers items to a listBox1 . Now I want to create a query that creates a loop from the listBox1 to SELECT FROM a table from SQL and add the result items to listBox2

I have this sample code but it's not working. Can someone help me?

public void add()
{
    var con = new DBConnection();
    try
    {
        for (int i = 0; i < listBServices.Items.Count; i++)
        {
            SqlCommand cmd = new SqlCommand("SELECT price FROM price WHERE service = '" +
                listBServices.Items.ToString() + "';", con.Connection);
            SqlDataReader rd = cmd.ExecuteReader();

            while (rd.Read())
            {
                int price = rd.GetInt32(0);
                listBPrice.Items.Add(price.ToString());
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

I get this exeption:

在此处输入图片说明

Check your connection and use the Using code block to close the connection automatically.

 string str = "Data Source=(local);Initial Catalog=Northwind;"
        + "Integrated Security=SSPI";
 string queryString =
        "SELECT price FROM price WHERE service ... ";

    using (SqlConnection connection =
               new SqlConnection(connectionString))
    {
        SqlCommand command =
            new SqlCommand(queryString, connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        // Call Read before accessing data.
        while (reader.Read())
        {
            ReadSingleRow((IDataRecord)reader);
        }

        // Call Close when done reading.
        reader.Close();
    }

You should close the reader after while

public void add()
{
    var con = new DBConnection();
    try
    {
        for (int i = 0; i < listBServices.Items.Count; i++)
        {
            SqlCommand cmd = new SqlCommand("SELECT price FROM price WHERE service = '" +
                listBServices.Items.ToString() + "';", con.Connection);
            SqlDataReader rd = cmd.ExecuteReader();

            while (rd.Read())
            {
                int price = rd.GetInt32(0);
                listBPrice.Items.Add(price.ToString());
            }

            rd.Close();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

listBServices.Items.ToString() results in the string "System.Windows.Forms.ListBox+ObjectCollection" . You must use

SqlCommand cmd = new SqlCommand("SELECT price FROM price WHERE service = '" + 
                                listBServices.Items[i] + "'",
                                con.Connection);

But using string concatenation is not a good idea. Use parameters instead.

SqlCommand cmd = new SqlCommand("SELECT price FROM price WHERE service = @svc",
                                con.Connection);
cmd.Parameters.Add("@svc", SqlDbType.NVarChar).Value = listBServices.Items[i];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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