我有一个SqlDataSource
用这个SelectCommand
给ListBox喂食
<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" />
效果很好。
我有这个TextBox
<asp:TextBox ID="tbPatient" runat="server" MaxLength="100" />
还有一个搜索按钮
<asp:Button ID="btnSearch" runat="server" Text="Search"
OnClick="btnSearch_Click" CausesValidation="false" />
现在,我想修改SelectCommand
,以便当用户单击btnSearch按钮时,它只搜索病人名称喜欢 (传递到tbPatient
文本框中)。
为了做到这一点,我查看了后面的代码并尝试:
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,显示,只有病人的名字,像一个输入文本框。我怎么才能解决这个问题?
发布于 2015-05-12 14:32:19
不知道这是不是问题所在,但是这个,
DSPatients.SelectParameters.Add("Ptrn", tbPatient.Text);
参数应该包括@符号,如下所示
DSPatients.SelectParameters.Add("@Ptrn", tbPatient.Text);
https://stackoverflow.com/questions/28647797
复制相似问题