Tried a query first like this
SELECT *
FROM CUSTOMER
WHERE LEFT(NAME, 1) = 'A'
which worked fine, but now I want it as a stored procedure and ‘A’ being replaced by a search term like this
CREATE PROCEDURE [dbo].[procCustomer_SelectByFirstLetter]
@SearchTerm VARCHAR(50)
AS
SELECT [Name]
FROM [dbo].[Customer]
WHERE LEFT(NAME, 1) = '@SearchTerm'
GO
this stored procedure is called by a C# method which uses a dropdown list with all the letters A-Z
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable customerTable = Customer.SelectByFirstLetter(Request.QueryString[DropDownList1.SelectedValue.ToString()]);
CustomerRepeater.DataSource = customerTable;
CustomerRepeater.DataBind();
CustomerCountLabel.Text = customerTable.Rows.Count.ToString();
}
the Reapter shows nothing though when I select a letter from the drop down. Any ideas? Thanks!
You’re still using the quoted value, so it looks for a name where the first letter equals the string
"@SearchTerm", which of course nothing ever will.You’ll need to remove the quotes:
Additionally, you may or may not be aware that you can achieve this with
LIKE:… Or
… Where the
%is wildcard for anything. In this way, passingAwill search for anything starting with the letter A, but passingABCwill search for anything with the letters “ABC”, without having to specify the length, as in yourLEFTfunction.Update
Additionally, you seem to be passing the parameter to the procedure incorrectly:
Here, you’re looking for a key in the QueryString collection, matching your selected value. So if
DropDownList1.SelectedValue.ToString()yields “A”, it’ll be looking for?A=.... You probably want to use the following instead: