Reputation: 1032
I want to connect a textbox with gridview when user enters a text it should check with Name column of gridview if name column contains those word only those record then should be shown. GridView already have Daatasource2 with which it showing all available records now by textbox I want to show only those records that are like with the enterd words. I searched a lot but that did't help am getting exception Must declare the scalar variable “@abcm” I already seen this link Must declare the scalar variable "@Name" but it did't help me out. this is html for textbox.
<%-- <input class="form-control" placeholder="Library Search" name="srch-term" id="srch-term" type="text" />--%>
<div class="input-group-btn">
<asp:LinkButton ID="btnRandom"
runat="server"
CssClass="btn btn-primary"
OnClick="btnsearch_Click" >
<span aria-hidden="true" class="glyphicon glyphicon-search"></span>
this one is for gridview.
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource2" Height="20px" Width="979px"
style="grid-template-rows:max-content;
scrollbar-arrow-color:aquamarine;
background-color:#ffd800;">
<EmptyDataTemplate>No results found.</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
</Columns>
</asp:GridView>
and these are two datasources.
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:FYPConnectionString %>" SelectCommand="SELECT * FROM [tblFiles]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FYPConnectionString %>" SelectCommand="SELECT [Name], [Date] FROM [tblFiles] WHERE ([Name] LIKE '%' + @abcm + '%')">
</asp:SqlDataSource>
Datasource2 is already connected to gridview.
here aspx.cs file code for search button
public void btnsearch_Click(object sender, EventArgs e) {
if (abcm.Text == "") { Response.Redirect("Library.aspx"); }
else
{
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
}
}
the problem is am getting exception Must declare the scalar variable "@abcm"
Upvotes: 1
Views: 1389
Reputation: 981
The exception says it all. You need to define @abcm as SelectParameters before binding.
public void btnsearch_Click(object sender, EventArgs e) {
if (abcm.Text == "") { Response.Redirect("Library.aspx"); }
else
{
GridView1.DataSourceID = "SqlDataSource1";
SqlDataSource1.SelectParameters.Add("abcm", abcm.Text);
GridView1.DataBind();
}
}
Upvotes: 2
Reputation: 233
The data SQL string contains @abc. When passing a parameter to SQL it is usual for that to be a Stored Procedure with parameters. I think the connection method you are using required static SQL.
Here's how to do it ... ASP.NET C#: SqlDataSource with Stored Procedure and Parameters
Upvotes: 1