【发布时间】:2015-02-21 16:02:01
【问题描述】:
我有一个SqlDataSource 用这个SelectCommand 提供一个列表框:
<asp:SqlDataSource ID="DSPatients"
ConnectionString="<%$ ConnectionStrings:CSMain %>"
ProviderName="<%$ ConnectionStrings:CSMain.ProviderName %>"
SelectCommand="SELECT Id, dbo.PatientDescriptive(Id) Descriptive FROM Patient ORDER BY Id"
...
runat="server">
<asp:ListBox Rows="10" ID="patientsList" DataSourceID="DSPatients" DataValueField="Id" DataTextField="Descriptive" AutoPostBack="false" runat="server" />
效果很好。
我有这个文本框
<asp:TextBox ID="tbPatient" runat="server" MaxLength="100" />
还有一个搜索按钮
<asp:Button ID="btnSearch" runat="server" Text="Search"
OnClick="btnSearch_Click" CausesValidation="false" />
现在,我想修改SelectCommand,以便当用户单击btnSearch 按钮时,它只搜索传入tbPatient 文本框的患者姓名LIKE。
为此,我去了后面的代码并尝试了:
protected void btnSearch_Click(object sender, EventArgs e)
{
DSPatients.SelectParameters.Add("Ptrn", tbPatient.Text);
DSPatients.SelectCommand = "SELECT Id, dbo.PatientDescriptive(Id) Descriptive FROM Patient WHERE Name LIKE @Ptrn ORDER BY Id";
DSPatients.Select(DataSourceSelectArguments.Empty); // Is the problem here? What I have to put inside?
DSPatients.SelectParameters.Clear();
}
运行时出现以下错误:
异常详细信息:System.Data.SqlClient.SqlException:必须声明 标量变量“@Ptrn”。
我希望 ListBox 仅显示在文本框中输入的名称为 LIKE 的患者。我该如何解决这个问题?
【问题讨论】:
标签: c# asp.net sql-server parameters sqldatasource