ASP.net – TextBox input to filter Gridview

I have a GridView combine with data from SQL Server.

I want the GridView to show all the Movies and customers names and last names, by choosing the first letter of the movie only. The user will input the first letter in the TextBox.

I think <asp:ControlParameter can be used here but I don’t know how.

Here its design:

ASP.net - TextBox input to filter Gridview

Searching the forum, I found something like this solution, but it gave me an error – System.NullReferenceException:

    protected void Button1_Click(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(TextBox1.Text))
    {
        (GridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("FirstName LIKE '{0}%'", TextBox1.Text);
    }
}

ASP.net - TextBox input to filter Gridview

Will be glad for some clues,

Thank you in advance!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I found the solution – how to use <asp:ControlParameter :

<asp:SqlDataSource ID="SqlDataSource_Movie" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>" SelectCommand="(long generated code, than)--> WHERE (MovieName LIKE @Text + '%') ORDER BY FirstName ASC">
        <SelectParameters>    <asp:ControlParameter  ControlID="TextBox1" Name="Text"/>   </SelectParameters>
        </asp:SqlDataSource>

Now I can input the first letter of the Movie and it will filter the GridView to show all the users that rent movies which starts with this letter.

Thanks


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x