Friday, February 24, 2012

Change SqlCommand Based On Combo Box Selection

I have a textbox, combo box, and a button on a form. I would like to perform a different query depending on the combo box selection. I thought I could do something such as:

if (cboSearch.Text =="Selection1") { scCmd ="SELECT * FROM tblTable WHERE txtSearch = @.Selection1"; }else if (cboSearch.Text =="Selection2") { scCmd ="SELECT * FROM tblTable WHERE txtSearch = @.Selection2"; }else { scCmd ="SELECT * FROM tblTable WHERE txtSearch = @.Selection3"; }
However, this obviously does not operate as I would need it to. What is the proper method for conditional SqlCommand statements like this?

You need to pass the value in as a Control Parameter.

http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.controlparameter.aspx

No comments:

Post a Comment