Tag: asp.net

Pass Data to Partial View – ASP.NET MVC

Author: | Categories: Programming No Comments
There are many ways to pass Data to the Partial View. I’ll discuss here method which I use in my projects which is passing Strongly Typed Model to the View. View Models public class ParentViewModel { public Id { get; set; } ..... public ChildViewModel Child { get; set; } } public class ChildViewModel { […]

Navigation Menu using ViewModel ASP.NET MVC

Author: | Categories: Programming 7 Comments
There are many approaches to Generate Dynamic Navigation Menus in ASP.NET MVC Applications, in this post i will discuss one approach using ViewModels and Partial Views which I use in my Applications. First I’ll create a Hard coded Navigation then replace it with based on ViewModel. Lets say we want to create a menu like […]

Login with User Name instead of Email in ASP.NET MVC Identity

Author: | Categories: Programming 2 Comments
When an ASP.NET MVC Application is created with “Individual Accounts” authentication, it is set to use Email address to Register and Sign in instead of User Name, so in this Post I’ll show how to use User Name to Register and Login. Open AccountViewModels.cs in Models folder, add below code in LoginViewModel and RegisterViewModel [Required] […]

ASP.NET download big file using BinaryWrite

Author: | Categories: Programming No Comments
There are situation when you need to provide download option for a big file located somewhere on server or generated at runtime. Below function could be used to download files of any size. Sometimes downloading big file throws exception OutOfMemoryException showing “Insufficient memory to continue execution of the program”. So this function also handle this […]

Log4net logging in asp.net MVC

Author: | Categories: Programming No Comments
Make changes in web.config file as follows <configuration> <configSections> ... <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <appSettings> ... <add key="log4net.Config" value="log4net.config"/> <add key="log4net.Config.Watch" value="True"/> </appSettings> <log4net> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="D:/Logs/TTG SmartANPR REST Service/smartanprrest_%date{ddMMyyyy}.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" […]

WebClient with Timeout Support

Author: | Categories: Programming No Comments
By default the C# WebClient class does not let you modify the default timeout value of 1 minute. This subclass exposes the Timeout property so you can change the default without having to use a HttpWebRequest. using System; using System.Net; namespace MyNamespace { public class WebClientEx : WebClient { public int Timeout { get; set; […]

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