Friday, June 23, 2017

Search in XML with like Keyword

If you want to search in XMl valued that contained that specific string then use this query

SELECT *
FROM table_name
WHERE Col_name.exist('//*/text()[contains(upper-case(.),upper-case("zipcode"))]') = 1 --
in this query i am search zipcode in XML


Tuesday, June 20, 2017

How to Perform Sorting in Gridview in ASP.NET

So if you want to sort out the record by using a ASP.net GridView you have to do  2 thing for
ASPX 

   <asp:GridView ID="GVReport" runat="server" GridLines="Vertical"
                                            AutoGenerateColumns="False"  PageSize="20"
                                             OnSorting="GVReport_Sorting">
    <asp:TemplateField HeaderText="Approved By"  SortExpression="Approved">
                                                    <ItemTemplate>
                                                     <%# Eval("Approved") %>
                                                    </ItemTemplate>
</asp:GridView>

If the  AutoGenerateColumns="True"  then no need to define  for sort columns it automatically bind with each columns, and If you are using
TemplateField then you need to mention what columns you want to sort out , one thing make sure that in SortExpression it should be the bind column name.
OnSorting  We need to define a function that in our server side code.

So on server side you need to do the following steps , I am not going through the database connectivity and Dataset etc .. I assume that you know all these stuff


protected void GVReport_Sorting(object sender, System.Web.UI.WebControls.GridViewSortEventArgs e)
{
DataSet ds = new DataSet();
ds = getDataSet();

DataView dataView = default(DataView);
dataView = new DataView(ds.Tables(0));
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortExpression);

GVReport.DataSource = dataView;
GVReport.DataBind();
}



private string ConvertSortDirectionToSql(string sortExpression)
{
if (ViewState("GVReport_" + sortExpression) == null | ViewState("GVReport_" + sortExpression) == "DESC") {
ViewState("GVReport_" + sortExpression) = "ASC";
return "ASC";
} else {
ViewState("GVReport_" + sortExpression) = "ASC";
ViewState("GVReport_" + sortExpression) = "DESC";
return "DESC";
}
}


So as you can see the ConvertSortDirectionToSql function is what is doing If the ViewState is null It apply Ascending order other wise descending