Hello,I'm trying to filter gridview with query written behind code of page, which not works. I'm working with parameters I have tested code as query in sql server, and from there works fine. I'm not sure what I'm doing wrong.What works, what does not.This Select code works.Imports System.Data Imports System.Data.SqlClient Partial Class zest Inherits System.Web.UI.Page Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click SqlDataSource1.SelectCommand = "Select * From test Where FirstName = @FirstName and SecondName = @SecondName" SqlDataSource1.SelectParameters.Add("FirstName", DropDownListFirstName.Text) SqlDataSource1.SelectParameters.Add("SecondName", DropDownListSecondName.Text) SqlDataSource1.DataBind() End Sub Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load SqlDataSource1.SelectParameters.Clear() End Sub End Class
Then I was try to extend sql, and code not works, as query not show any results. Gridview is empty. Test query from sql server return all records, and no idea why not works here.Imports System.Data Imports System.Data.SqlClient Partial Class zest Inherits System.Web.UI.Page Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click SqlDataSource1.SelectCommand = "Select * From test Where FirstName = @FirstName and SecondName = @SecondName OR @FirstName IS NULL AND @SecondName IS NULL" SqlDataSource1.SelectParameters.Add("FirstName", DropDownListFirstName.Text) SqlDataSource1.SelectParameters.Add("SecondName", DropDownListSecondName.Text) SqlDataSource1.DataBind() End Sub Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load SqlDataSource1.SelectParameters.Clear() End Sub End Class
Here is also page.aspx.<form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <br /> <asp:DropDownList ID="DropDownListFirstName" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem>John</asp:ListItem> <asp:ListItem>Paul</asp:ListItem> <asp:ListItem>James</asp:ListItem> </asp:DropDownList> <br /> <asp:DropDownList ID="DropDownListSecondName" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem>Brown</asp:ListItem> <asp:ListItem>White</asp:ListItem> </asp:DropDownList> <br /> <asp:DropDownList ID="DropDownListTown" runat="server"> <asp:ListItem></asp:ListItem> <asp:ListItem>New York</asp:ListItem> <asp:ListItem>London</asp:ListItem> <asp:ListItem>Paris</asp:ListItem> </asp:DropDownList> <br /> <asp:Button ID="Button1" runat="server" Text="Button" /> <br /> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display."> <Columns> <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" /> <asp:BoundField DataField="SecondName" HeaderText="SecondName" SortExpression="SecondName" /> <asp:BoundField DataField="Town" HeaderText="Town" SortExpression="Town" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:database21ConnectionString %>" SelectCommand="SELECT [FirstName], [SecondName], [Town] FROM [test]"> </asp:SqlDataSource> </div> </ContentTemplate> </asp:UpdatePanel> </form>
Thanks in advance for help!