Posts

Donate

Show Or Get IP Address in ASP.NET MVC

Here's a simple tutorial on how to get a user's IP address using ASP.NET MVC and jQuery. The fetching of record is triggered when the textbox receives focus. HTML Code 1 <input id= "txtID" type= "text" /> JavaScript Code 1 2 3 4 5 6 7 8 $(document).ready( function () { $( '#txtID' ).focus( function () { $.getJSON( '@Url.Action("GetIPAddress","getipaddress")' , function (result) { $( "#txtID" ).val(result); }); }); }); C# Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public JsonResult GetIPAddress() { System.Web.HttpContext context = System.Web.HttpContext.Current; string ipAddress = context.Request.ServerVariables[ "HTTP_X_FORWARDED_FOR" ]; string output = string .Empty; if (! string .IsNullOrEmpty(ipAddress)) { string [] addresses = ipAddress.Split( ',' )

Operating System Error 5(Access is denied.) "Cannot open backup device" in SQL Server

Image
Here's how I manage to solve the issue Cannot open backup device with status Operating system error 5(Access is denied.) Steps 1. Right Click the Folder which is the backup destination 2. Click Properties -> Security Tab -> Click Advanced Button 3. On Permissions Tab -> choose Add -> Select Principal 4. In the "Enter the object name to select", type " NT Service\MSSQLSERVER ". -> OK     Note: NT Service\MSSQLSERVER is the Log On As value of     MSSQLSERVER service under Services. See screenshot below 5. In Multiple Names Found choose MSSQLSERVER. 6. Click OK 7. In Basic permissions panel, Choose Full Control 8. Click Apply -> OK -> OK to close Folder Properties dialog box SQL Server Service

How To Unpivot In SQL Server With Example

Image
Given the following records below, you may want to show them in a vertical manner instead of horizontal view of columns. To achieve the desired result, you use UNPIVOT clause in your query. 1 2 3 4 5 6 7 8 Use testdatabase Go SELECT [Subject], Grade FROM StudentReportCard UNPIVOT (Grade FOR [Subject] IN (English, Math, Literature, Biology)) AS U_SubjectGrade WHERE StudentId = 1 Screenshot

Webbrowser GetAttribute("class") Method Not Working

When working with Webbrowser's GetAttribute() method, you often encountered an issue wherein GetAttribute("class") does not work as expected. 1 2 3 if (Element.GetAttribute( "class" ).Contains( "_textFontColor" )) { //other codes here } After investigating from MSDN docs, I found out a comment below the documentation which states that you have to use className instead of class. Well, that did the trick. :-) 1 2 3 4 5 6 7 8 9 10 11 12 13 HtmlElementCollection elements = WebBrowser1.Document.GetElementsByTagName( "A" ); foreach (HtmlElement Element in elements) { if (Element.GetAttribute( "className" ).Contains( "_textFontColor" )) { //other codes here } } HtmlElementCollection elements = WebBrowser1.Document.GetElementsByTagName( "a" ); foreach (HtmlElement Element in elements) { if (Element.GetAttribute( "className" ).Contains( "_textFontColor" ))

Bootstrap DatePicker Control In ASP.NET Web Forms

Image
Here's how to integrate Bootstrap DatePicker widget in you ASP.NET Webforms Project. First, you need to add reference to the following JS and CSS files: bootstrap.min.css , bootstrap-datepicker3.standalone.css , jquery-1.11.1.min.js , bootstrap.min.js , bootstrap-datepicker.min.js . 1 2 3 4 5 <link rel= "stylesheet" href= "Content/bootstrap.min.css" /> <link rel= "stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker3.standalone.css" /> <script type= "text/javascript" src= "Scripts/jquery-1.11.1.min.js" ></script> <script type= "text/javascript" src= "Scripts/bootstrap.min.js" ></script> <script type= "text/javascript" src= "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.1/js/bootstrap-datepicker.min.js" ></script> Create a simple registration form with B

WPF DataGrid Excel Comment Indicator

Image
Here's how to add a comment indicator to a DataGridCell similar to an Excel functionality using MultiValueConverter. See codefiles and screenshot below. XAML 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 <Window x:Class= "WPFExcelCommentIndicator.DataGridCellCommentIndicator" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src= "clr-namespace:WPFExcelCommentIndicator" Title= "DataGridCellCommentIndicator" Height= "350" Width= "500" WindowStartupLocation= "CenterScreen" > <Window.Resources> <src:DataGridCellComment x:Key= "DataGridCellComment" /> <src:StudentList x:Key= "StudentListData" /> </W

How To Convert DataTable To List Using LINQ And C#

Hello, The code below converts a DataTable object to generic List object using LINQ and C#. It assumes that the DataTable columns match with class properties. C# Code 1 2 3 4 5 6 7 8 var personEnumerable = table.AsEnumerable(); List<Person> ListPosition = new List<Person>(); ListPosition = ( from item in personEnumerable select new Person { ID = item.Field< int >( "ID" ).ToString(), Salary = item.Field< double >( "Salary" ).ToString() }).ToList(); Class 1 2 3 4 5 public class Person { public string ID { get ; set ; } public string Salary { get ; set ; } }

CookieContainer - Part Of Cookie Is Invalid In C# Cookies And WebResponse

Given the code below when scraping a website, you might encounter an error which is stated in the title of the post that is Part Of Cookie Is Invalid . 1 2 3 4 5 6 7 8 9 10 11 WebRequest request = WebRequest.Create( "http://your_url_here" ); WebResponse webResponse = request.GetResponse(); if (webResponse.Headers[ "Set-Cookie" ] != null ) { Cookie m_ccCookies = new Cookie(); CookieContainer ccContainer = new CookieContainer(); ccContainer = new CookieContainer(); ccContainer.SetCookies(webResponse.ResponseUri, webResponse.Headers[ "Set-Cookie" ]); ccContainer.Add(ccContainer.GetCookies(webResponse.ResponseUri)); } The issue could be that the url domain cookie value which is passed to SetCookies method have unrecognized characters that needs to be encoded. So, the workaround is to encode the cookie value first and then assign the appropriate encoding before saving it to the cookie container object. 1 2 ccC

Get Total Hours And Minutes From Total Minutes Using TimeSpan Class In C# And VB.NET

Image
Good evening! Given total number of minutes as input and you need to extract the total number of hours and remaining minutes out from the input, you need to use the TimeSpan class specifically FromMinutes() method. Based from the docs ,the FromMinutes method returns a TimeSpan object that represents a specified number of minutes, where the specification is accurate to the nearest millisecond. C# Code 1 2 3 4 TimeSpan span = TimeSpan.FromMinutes(1506); var hours = ( int ) span.TotalHours; var minutes = span.Minutes; Console.WriteLine(hours + ":" + minutes.ToString( "D2" )); VB.NET Code 1 2 3 4 Dim span As TimeSpan = TimeSpan.FromMinutes(1506) Dim hours = CInt (span.TotalHours) Dim minutes = span.Minutes Console.WriteLine(hours + ":" + minutes.ToString( "D2" )) Output

ASP.NET MVC ListBoxFor() With Optgroup Tag Support

Image
Here's a simple ListBoxFor helper that supports optgroup tag. See versions for C# and VB.NET. C# Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public static class HtmlExtensions { public static IHtmlString ListBoxFor<TModel, TProperty>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Dictionary< string , IEnumerable<SelectListItem>> selectList, object htmlAttributes = null ) { var select = new TagBuilder( "select" ); select .Attributes.Add( "name" , ExpressionHelper.GetExpressionText(expression)); if (htmlAttributes != null ) { RouteValueDictionary routeValues = new RouteValueDictionary(htmlAttributes); if (!routeValues.ContainsKey(( "size" ).ToLower(

Donate