Posts

C# coalesce operator : Question Mark In C# and Double Question Mark

A not so common, but very useful operator is the double question mark operator (??). This can be very useful while working with null able types. Lets say you have two nullable int: int? numOne = null; int? numTwo = 23; Note : int? (data type with question mark used to declare variable with null able type) Scenario: If numOne has a value, you want it, if not you want the value from numTwo , and if both are null you want the number ten (10). Old solution: if (numOne != null)     return numOne; if (numTwo != null)     return numTwo; return 10; Or another solution with a single question mark : return (numOne != null ? numOne : (numTwo != null ? numTwo : 10)); But with the double question mark operator we can do this: return ((numOne ?? numTwo) ?? 10); Output : return numOne if have value, else return numTwo else return 10; As you can see, the double question mark operator returns the first value that is not null. By Mohd Zulkamal NOTE : – If You have Found this post Helpful, I will a

Pinging in ASP.NET Example

Image
This tutorial will show you how to ping a hostname/ip using the .NET System.Net class, ASP.NET 2.0 and C#.NET. The .NET Framework offers a number of types that makes accessing resources on the network easy to use. To perform a simple ping, we will need to use the System.Net, System.Net.Network.Information, System.Text namespaces. using System.Net; using System.Net.NetworkInformation; using System.Text; ASPX Page  <asp:ScriptManager ID="ScriptManager1" runat="server">     </asp:ScriptManager>     <asp:UpdatePanel ID="UpdatePanel1" runat="server">         <ContentTemplate>             <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">                 <tr>                     <td width="100" align="right" bgcolor="#eeeeee" class="header1">                         Ho

Dynamically Add/Remove rows in HTML table using JavaScript

Image
This is very often for web developer when we need to use dynamically generate HTML table using JavaScript. The example will spare the first row(cannot delete) and you can dynamically add new row by clicking the button Add Row or delete the row by clicking the button Delete Row . The Code <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     <title>Add/Remove dynamic rows in HTML table </title>     <script language="javascript">            function addRow(tableID) {             var table = document.getElementById(tableID);             var rowCount = table.rows.length;             var row = table.insertRow(rowCount);             var cell1 = row.insertCell(0);             var element1 = document.createElement("input");             element1.type = "checkbox";             ce

Constants VS ReadOnly

Constants are similar to read-only fields. You can’t change a constant value once it’s assigned. The const keyword precedes the field to define it as a constant. Assigning value to a constant would give a compilation error. For example: const int num3 = 34; num3 = 54; // Compilation error: the left-hand side of an assignment must  // be a variable, property or indexer Although constant are similar to read-only fields, some differences exist. Constants are always static, even though you don’t use the static keyword explicitly, so they’re shared by all instances of the class. The readonly keyword differs from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Example Constant public class ConstTest {     class SampleClass     {         public int x;         public int y;         p

C#: Execute Two Dependant Processes

This example shows how to run 2 program in application and the second program need to run after the first program successfully start. The example will use 2 program which is Notepad and Paint . The Notepad need to run first and after Notepad successfully run, the Paint application will start to open program. Let see the code. Two Dependant Processes using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Threading; namespace TwoDependantProcesses {      class Program     {           static void Main(string[] args)          {                  Console.WriteLine("Press Enter to start Notepad (i.e. Process 1)");                  Console.ReadKey();                  Console.WriteLine("Starting Notepad...");                  Thread t1 = new Thread(new ThreadStart(StartProcess1));                   t1.Start();                   while (t1.IsAlive == true)                  {                       System.Threading.Thread.Sleep

fancybox on google map marker example

Image
FancyBox is a tool for displaying images, html content and multi-media in a Mac-style "lightbox" that floats overtop of web page.  This example will show how to use Google Map Marker With Fancybox. You can see my previous post about Creating Google Map API Zoom the marker This is the requirement for this example : Jquery Script - you can get it here fancybox script pack - you can get it here fancybox css - you can get it here html page and of course the Google Map Script Header on html page * The header should link with jquery and fancybox script. I will put Google Marker script at header also. <script type="text/javascript" src="jquery-latest.min.js"></script> <script type="text/javascript" src="jquery.fancybox.pack.js"></script> <link href="jquery.fancybox.css" type="text/css" rel="StyleSheet" /> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor

How to remove a MenuItem base on condition

There may be an easier solution, which is to use the MenuItemDataBound event. I use this to hide menu nodes from the Menu Controller . Keeping them in the siteMap allows them to appear in the Menu Controller , but I don't want them in the Menu Controller based on the Roles attribute . Here's my code: The SiteMap File <?xml version="1.0" encoding="utf-8" ?> <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >     <siteMapNode url="" title=""  description="">         <siteMapNode url="#" title="Home" roles="*"  description="This is Homepage" />         <siteMapNode url="" title="About" roles="*"  description="This About Page" />         <siteMapNode url="_#" title="Product" roles="ADMIN" description="This is Product Page, Only Role Admin Can access"