Category: Programming

All about programming

JQUERY AJAX different ways

Author: | Categories: Programming No Comments
Below are different ways to perform AJAX Get and Post requests through jQuery. GET request method 1 $.ajax({ url: url, data: data, success: success, dataType: dataType }); GET request method 2 $.get( "ajax/test.html", function( data ) { $( ".result" ).html( data ); alert( "Load was performed." ); }); GET request method 3 – jqXHR // […]

Waiting for AJAX response

Author: | Categories: Programming No Comments
Below code will fade out all elements in a web page till AJAX completion of request. $("body").ajaxSend(function() { $(this).append('<div id="loading">Data Loading</div>'); $('#tag').children().not('#loading').css({'opacity':0.22}); }); $("body").ajaxComplete(function() { $("div#loading").remove(); $(#tag).children().not('#loading').css({'opacity':1}); });

ASP.NET GridView Paging and Sorting

Author: | Categories: Programming No Comments
Below is the sample to include Sorting and Paging in ASP.Net GridView. ASPX Code <asp:GridView ID="grd" runat="server" AutoGenerateColumns="False" AllowSorting="true" OnSorting="grd_Sorting" AllowPaging="true" PageSize="50" OnPageIndexChanging="grd_PageIndexChanging"> Code Behind protected void grd_Sorting(object sender, GridViewSortEventArgs e) { try { mDT = GetData(); SetSortDirection(SortDirection); if (mDT != null) { mDT.DefaultView.Sort = e.SortExpression + " " + _sortDirection; grd.DataSource = mDT; grd.DataBind(); […]

ASP.NET AJAX File Upload

Author: | Categories: Programming No Comments
jQuery can help in uploading a file on asp.net web page asynchronously. Add a generic handler in asp.net project like below. Upload Generic Handler – Code behind file (upload.ashx) public class upload : IHttpHandler { public void ProcessRequest(HttpContext context) { string fileName = HttpContext.Current.Request.QueryString["FileName"].ToString(); using (FileStream fs = File.Create("[Path]\\" + fileName)) { Byte[] buffer = […]

C# snippets

Author: | Categories: Programming No Comments
Converting a string to byte-array without using an encoding (byte-by-byte) static byte[] GetBytes(string str) { byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); return bytes; } static string GetString(byte[] bytes) { char[] chars = new char[bytes.Length / sizeof(char)]; System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length); return new string(chars); } Using String Format to […]

C# DataTable select method

Author: | Categories: Programming One Comment
DataTable has a Select method. This method receives a string expression that specifies what rows you want to handle. Selection based on a string expression DataRow[] result = table.Select("Size >= 230 AND Sex = 'm'"); Selection based on DateTime DataRow[] result = table.Select("Date > #6/1/2001#"); Number of records for a particular criteria int numberOfRecords = […]

DataTable to HTML Table C# code

Author: | Categories: Database, Programming No Comments
C# DataTable to a HTML Table Below is C# function to generate HTML table string from DataTable with custom options like Custom headers and Selected fields. “Format” in parameters will contain comma separated values like column1=column 1 name, column2=column 2 name and so on. public static string DataTableToHtmlTable(DataTable TableData, string Format) { try { Dictionary<string, […]

Datatable to CSV file

Author: | Categories: Database, Programming No Comments
C# DataTable to a CSV file C# DataTable to a CSV file with default options i.e. all columns with default column names public static string DataTableToCsvFile(DataTable TableData, string FileName) { try { // change this with any delimiter you want string Delimiter = ","; int i = 0; StreamWriter sw = null; sw = new […]

Formatting ASP.net GridView

Author: | Categories: Programming No Comments
Formatting ASP.net GridView BoundField For Date and Time DataFormatString="{0:d-MMM-yyyy}" For Decimals DataFormatString="{0:F2}" For Currency DataFormatString="{0:C2}" For Percentage DataFormatString="{0:P2}"

SQL Snippets

Author: | Categories: Database, Programming No Comments
Rounding off to 2 decimal places cast(round([value],2) as decimal(18,2)) Compare only Year from datetime field select * from [table_name] where year(date_time_field) = year(getdate()) check if a SQL server string is null or empty ISNULL(NULLIF(text, ''), '') AS Offer_Text The differences between LEN and DATALENGTH in SQL Server LEN Returns the number of characters, rather than […]