I have Search Panel with TextBox and Two DropDownList (txtKeyword, ddlCategory, ddlProvider), i want search with one of them or two together or all, so i used COALESCE Statement..
(
@Course NVARCHAR(200) = NULL,
@Category NVARCHAR(50) = NULL,
@Provider NVARCHAR(200) = NULL
)
AS
BEGIN
SELECT Course.CourseId, Course.CourseName, Course.CourseDescription,
Course.CourseHours, Category.CategoryName, Provider.ProviderName,
CourseType.CourseTypeName
FROM Course INNER JOIN
Category ON Course.CategoryId = Category.CategoryId INNER JOIN
Provider ON Course.ProviderId = Provider.ProviderId INNER JOIN
CourseType ON Course.CourseTypeId = CourseType.CourseTypeId
WHERE (Course.CourseName = COALESCE (@Course, Course.CourseName)) AND
(Category.CategoryName = COALESCE (@Category, Category.CategoryName)) AND
(Provider.ProviderName = COALESCE (@Provider, Provider.ProviderName))
END
-------------------------------------------------------
Then used it with SqlDataSource and GridView
<asp:GridView ID="gvUserSearch" runat="server" DataKeyNames="CourseID"
Width="100%" AllowPaging="True" EnableViewState="False"
AutoGenerateColumns="False">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="CourseID" HeaderText="Course ID" />
<asp:BoundField DataField="CourseName" HeaderText="Name" />
<asp:BoundField DataField="CourseDescription" HeaderText="Description" />
<asp:BoundField DataField="CourseHours" HeaderText="Hours" />
<asp:BoundField DataField="CategoryName" HeaderText="Category" />
<asp:BoundField DataField="ProviderName" HeaderText="Provider" />
<asp:BoundField DataField="CourseTypeName" HeaderText="Course Type" />
</Columns>
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="dsCourses" runat="server"
ConnectionString="<%$ ConnectionStrings:ShareCon %>" SelectCommand="sp_GetAllCourses"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter ConvertEmptyStringToNull="true" Name="Course"
Type="String" QueryStringField="CourseName" />
<asp:QueryStringParameter ConvertEmptyStringToNull="true" Name="Category"
Type="String" QueryStringField="Category" />
<asp:QueryStringParameter ConvertEmptyStringToNull="true" Name="Provider"
Type="String" QueryStringField="Provider" />
</SelectParameters>
</asp:SqlDataSource>
--First-- in Main Page that contains (TextBox and DropDownLists only) (there's six states this one of them)
Response.Redirect("SearchResults/Default.aspx?CourseName=" +
"&Category=" + "&Provider=");
i tried this too..
Response.Redirect("SearchResults/Default.aspx?CourseName=NULL" +
"&Category=NULL" + "&Provider=NULL");
--nd-- in Default.aspx
gvUserSearch.DataSourceID = "dsCourses";
gvUserSearch.DataBind();