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
try: strSQL = "SELECT COUNT(*) as count from MyTable where ID=' " + MyLabel.Text + " ' "; Or better yet use parameters (see Colin Mackay's article here[^]) for reasons and examples. We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest. Patricia Aburdene
-
try: strSQL = "SELECT COUNT(*) as count from MyTable where ID=' " + MyLabel.Text + " ' "; Or better yet use parameters (see Colin Mackay's article here[^]) for reasons and examples. We need to graduate from the ridiculous notion that greed is some kind of elixir for capitalism - it's the downfall of capitalism. Self-interest, maybe, but self-interest run amok does not serve anyone. The core value of conscious capitalism is enlightened self-interest. Patricia Aburdene