icsd12015
icsd12015

Reputation: 33

TextBox.Text Updated On DropDownList_TextChanged

I have a DropDownList connected with an sqlDataSource and a TextBox . I want every time the user selects a name from the column name listed on the DropDownList, the value of the column id of that item being displayed on the text of the TextBox

I made this code but doesnt seems to work: (the code contains no errors)

protected void DropDownListIliaka_SelectedIndexChanged(object sender, EventArgs e)
{
        string conString = "Data Source=icsd-db.aegean.gr\\icsdmssqlsrv;Initial Catalog=icsd12015;Integrated Security=True;";
        SqlConnection con = new SqlConnection(conString);

        string cmdText = "SELECT iliako_sistima_ID FROM iliako_sistima WHERE name = '" + DropDownListIliaka.Text + "'";
        SqlCommand cmd = new SqlCommand(cmdText, con);

        try
        {
            con.Open();

            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    iliako_sistima_id.Text = (reader["Iliako_Sistima_ID"].ToString());
                }
            }
        }
        finally
        {
            con.Close();
        }

}

Upvotes: 0

Views: 56

Answers (2)

Karthik Ganesan
Karthik Ganesan

Reputation: 4222

this

 string cmdText = "SELECT iliako_sistima_ID FROM iliako_sistima WHERE name = '" + DropDownListIliaka.Text + "'";

should be

string cmdText = "SELECT iliako_sistima_ID FROM iliako_sistima WHERE name = '" + DropDownListIliaka.SelectedItem.Text + "'";

Hope this helps

Upvotes: 2

dotnetom
dotnetom

Reputation: 24916

You must set the property AutoPostBack of DropDownList to true, so it posts back to the server and sets the value of textbox

Upvotes: 0

Related Questions