1

I've been trying to run sql query if the user selects item from dropdownlist and put the sql result in a textbox.

The dropdown menu shows mobile models names.Onclick of a model the price should be retrieved and shown in textbox.

I have setup the dropdownlist to load models from the table "Products"

 DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("select Price from Products WHERE Model='" +DropDownList1.SelectedIndexChanged+ "'", con);

 da.Fill(dt);
        TextBox1.Text = dt.Rows[0]["Price"].ToString();

But nothing shows up in the textbox even if i click an item from the dropdown

3
  • Are you getting values for the data set. Try to debug the code. Commented Apr 17, 2017 at 11:34
  • yes,i can see the values Commented Apr 17, 2017 at 11:36
  • WebForm or WinForm? Commented Apr 17, 2017 at 11:38

2 Answers 2

1

If you are using ASP.NET, then you have to set the AutoPostBack of the dropdownlist to True. Then execute your code in the dropdownlist_selectedindexchanged

Sign up to request clarification or add additional context in comments.

3 Comments

Works like a charm.Thank you so much @Abusnake
but can you tell me what autopostback does?
Autopostback allows the control to automatically post the page to the server when event occures, in your case, when the selected item change
0

Your SQL command is invalid. It should be

SqlDataAdapter da = new SqlDataAdapter("select Price from Products WHERE
Model='" +DropDownList1.SelectedText+ "'", con);

Either

DropDownList1.SelectedValue 

or

DropDownList1.SelectedText

It Should not be

DropDownList1.SelectedIndexChanged

2 Comments

Yes,i have changed it to DropDownList1.SelectedValue .Thankyou so much @Harsha W
Without AutoPostBack it dosent work,hence @Abusnake's answer is right.Thankyou.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.