简体   繁体   中英

How do i input a value from a databse into a textbox when selecting a value from a dropdown list

I currently have a dropdown list which is populated with locations from a location table in my database. The location table alco contains a "Miles" column.

What i am trying to do is once a location is selected in the dropdown list the textbox will then be populated with the miles.

I have tried this code;

public string conString = "Data Source=DESKTOP-86TC1QA\\MSSSQLSERVER17;Initial Catalog=AttendanceApp;Integrated Security=True";

    private void location_comboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand("SELECT * FROM Location_Table WHERE Miles = '" + location_comboBox.Text + "'",con);
        con.Open();
        cmd.ExecuteNonQuery();
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        while (dr.Read())

        {
            var Miles = (string)dr["Miles"].ToString();
            mileage_textbox.Text = Miles;
        }
        con.Close();
    } 

but is generated the following error and i am unsure how to resolve it.

System.Data.SqlClient.SqlException: 'Error converting data type varchar to numeric.'

If you have noticed your select statement does not look correct. You are trying to get Miles, while you are passing Miles in where clause.

First you will have to change the name of the column. Assuming table has a column name Location then the query should look like below -

      SqlCommand cmd = new SqlCommand("SELECT * FROM Location_Table WHERE Location = 
      '" + location_comboBox.Text + "'",con);

Though, above change should work as per your expectation however this code is not recommened as it can create SQL Injection problem, which you may want to read about. The ideal way to pass the parameter should be as below -

        // 1. Create SQL Command  
        SqlCommand cmd = new SqlCommand("SELECT * FROM Location_Table WHERE Location 
        = @location", conn);

        // 2. define parameters used in above command 
        SqlParameter param  = new SqlParameter();
        param.ParameterName = "@location";
        param.Value         = location_comboBox.Text;

        // 3. add new parameter to command 
        cmd.Parameters.Add(param);

        // get data stream
        dr= cmd.ExecuteReader();

You can use this:

var conString = "Data Source=DESKTOP-86TC1QA\\MSSSQLSERVER17;Initial Catalog=AttendanceApp;Integrated Security=True";

using (var connection = new SqlConnection(connectionString)) {
    connection.Open();
    using (var command = new SqlCommand("SELECT * FROM Location_Table WHERE Location 
    = @location", connection)) {
        command.Parameters.Add(new SqlParameter("location", Int32.Parse(location_comboBox.SelectedValue.ToString());
        var reader = command.ExecuteReader();
        while (reader.Read()) {
            var Miles = (string)dr["Miles"].ToString();
            mileage_textbox.Text = Miles;
        }
    }
}

Its always a good idea to dispose off the resources. Also, location_combobox is a confusing name for a dropdown.

You have to use the following to get the selected value from dropdown and parse it to an integer.

Int32.Parse(dropdown.SelectedValue.ToString());

Please change the SQL query as below given query because miles seems to be numeric in SQL table:

SqlCommand cmd = new SqlCommand("SELECT * FROM Location_Table WHERE Miles = " + 
location_comboBox.Text, con);

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