Pass value from dropdownlist to SQL select using SelectedIndexChanged?
-
I want to pass a(numerical) value from a dropdownlist to an SQL select using the DropDownList SelectedIndexChanged event. Here's my code so far. protected void MyDropDownList_SelectedIndexChanged(object sender, EventArgs e) { DropDownList MyDropDownList = (DropDownList)("MyDropDownList"); MyLabel.Text = MyDefinitionDropDownList.SelectedItem.Value.ToString(); string strSQL; strSQL = "SELECT COUNT(*) as count from MyTable where ID= ???"; } My problem is that I don't know what should replace the '???'. I've tried strSQL = "SELECT COUNT(*) as count from MyTable where ID= MyLabel.Text"; but this is interpreted as "SELECT COUNT(*) as count from MyTable where ID= MyLabel.Text" even though my debugger tells me MyLabel.Text is grabbing an OK value. If I do strSQL = "SELECT COUNT(*) as count from MyTable where ID= 6" for example it works fine. I think my problem is with casting? Thanks Majella
-
I want to pass a(numerical) value from a dropdownlist to an SQL select using the DropDownList SelectedIndexChanged event. Here's my code so far. protected void MyDropDownList_SelectedIndexChanged(object sender, EventArgs e) { DropDownList MyDropDownList = (DropDownList)("MyDropDownList"); MyLabel.Text = MyDefinitionDropDownList.SelectedItem.Value.ToString(); string strSQL; strSQL = "SELECT COUNT(*) as count from MyTable where ID= ???"; } My problem is that I don't know what should replace the '???'. I've tried strSQL = "SELECT COUNT(*) as count from MyTable where ID= MyLabel.Text"; but this is interpreted as "SELECT COUNT(*) as count from MyTable where ID= MyLabel.Text" even though my debugger tells me MyLabel.Text is grabbing an OK value. If I do strSQL = "SELECT COUNT(*) as count from MyTable where ID= 6" for example it works fine. I think my problem is with casting? Thanks Majella
-
Hi, You do like this: strSQL = "SELECT COUNT(*) as count from MyTable where ID=" + MyLabel.Text; Hope this will solve the problem. :) Best Regards, Apurva Kaushal