Tag: c#

HTTP Status Code in case of Exception

Author: | Categories: Programming No Comments
This is how to get HTTP Status code in case of an Exception during HttpWebRequest catch(WebException ex) { HttpWebResponse res = (HttpWebResponse) ex.Response; Console.WriteLine("Http status code: " + res.StatusCode); }

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 […]

ASP.Net persist readonly textbox value on postback

Author: | Categories: Programming No Comments
As a security measure in the asp.net the readonly fields and disabled fields loose their values in postbacks. To retain the readonly textbox value after postback instead of making the textbox readonly using properties option, use attributes in code: TextBox1.Attributes.Add(“readonly”, “readonly”); that will still make the textbox readonly and will also retain the value after […]

ASP.NET calling Webmethod using jQuery

Author: | Categories: Programming No Comments
To call Webmethod asynchronously in ASP.net through jQuery Javascript Code <script type=”text/javascript”> function Authenticate() { $.ajax({ type: "POST", url: "/login.aspx/Authenticate", data: "{ provide json data }", contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { if (msg.d == '1') { alert(‘authenticated’); } else { alert("not authenticated"); } }, error: function() { alert("Error :("); } }); }; […]