简体   繁体   中英

Changing DataSource of gridview in aspx.cs File

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();
        }
    }

Now attaching a pic of output 在此输入图像描述

the problem is am getting exception Must declare the scalar variable "@abcm"

在此输入图像描述

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();
        }
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM