Tuesday, April 17, 2018

Starting an application is system tray using NotifyIcon without showing the window.

Starting an application is system tray using NotifyIcon without showing the window.

Requirement : To start an application directly from systemtray without showing the window. On double clicking the icon in systemtray it should open the URL in default web browser. On right clicking the icon in system tray a content menu should open and on clicking the same open the URL in default web browser.

Solution: Putting the below code in Program.cs will help you to achieve the same.

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (NotifyIcon icon = new NotifyIcon())
{
icon.Icon = ISBot.Properties.Resources.ISChatbot;
icon.ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Open", (s, e) => {System.Diagnostics.Process.Start("http://rajeshkamalakshan.blogspot.in"); }),
});
icon.Visible = true;
icon.DoubleClick += Icon_DoubleClick;
icon.Text = "Rajesh Kamalakshan";
Application.Run();
icon.Visible = false;
}
}

private static void Icon_DoubleClick(object sender, EventArgs e)
{
LaodInbrowser ();
}

private static void LaodInbrowser ()
{
System.Diagnostics.Process.Start("http://rajeshkamalakshan.blogspot.in");
}

Monday, April 16, 2018

How to open URL in default browser c#?


You can open a web page in Operating System’s Default browser by using below code.



System.Diagnostics.Process.Start("http://rajeshkamalakshan.blogspot.in/");

Wednesday, March 21, 2018

Deserialize / Serialize JSON using Newtonsoft.Json


Using Newtonsoft.Json its quiet easy to Deserialize / Serialize JSON. Below code snippet will show you how to do this. 

public partial class Window1 : Window
{
        public Window1()
        {
            InitializeComponent();
        }

        public class Product
        {
            public string Code { get; set; }
            public string Description { get; set; }
            public  List<Price> Price { get; set; }
        }
        public class Price
        {
            public decimal WholeSale { get; set; }
            public decimal Retail { get; set; }
        }
        public void Deserialize()
        {
            string json = @"[{'Code': 'P1','Description': 'Product1','Price' : [{'WholeSale':'11','Retail':'22'}]}
                            ,{'Code': 'P2','Description': 'Product2','Price' : [{'WholeSale':'33','Retail':'44'}]}
                            ]";
            var products = JsonConvert.DeserializeObject<List<Product>>(json);
        }
        public void Serialize()
        {
            var products = new List<Product>
            {
                new Product()
                {
                    Code = "P1",
                    Description = "Prodict1",
                    Price = new List<Price>()
                    {
                        new Price
                        {
                            Retail = 11,
                            WholeSale = 22
                        },
                    }
                },
                new Product()
                {
                    Code = "P2",
                    Description = "Prodict2",
                    Price = new List<Price>()
                    {
                        new Price
                        {
                            Retail = 33,
                            WholeSale = 44
                        },
                    }
                }
            };
            string json= JsonConvert.SerializeObject(products);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Deserialize();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Serialize();
        }
}


Thursday, December 14, 2017

How to solve “WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jQuery. Please add a ScriptResourceMapping named jQuery(case-sensitive)”


Error: “WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jQuery. Please add a ScriptResourceMapping named jQuery(case-sensitive)



This error comes when we use RequiredFieldValidator control on any other server control in asp.et web applictaion. We can solve this error by enabling pre 4.5 validation mode by adding an appsetting key in web.config.



 <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>



What happened when we add this key?


Adding this key specifies how ASP.NET globally enables the built-in validator controls to use unobtrusive JavaScript for client-side validation logic. As said here

Remarks: If this key value is set to "None" [default], the ASP.NET application will use the pre-4.5 behavior (JavaScript inline in the pages) for client-side validation logic. If this key value is set to "WebForms", ASP.NET uses HTML5 data-attributes and late bound JavaScript from an added script reference for client-side validation logic.




Tuesday, December 5, 2017

Using PowerShell to list COM+ application and the identity on which they run

Blow PowerShell script will list down the com+ application running on your machine and the identity on which they run

$comAdmin = New-Object -com ("COMAdmin.COMAdminCatalog.1")
$applications = $comAdmin.GetCollection("Applications")
$applications.Populate()
foreach ($application in $applications)
{
    $application.Value("Name") + " " + $application.Value("Identity")
}


How to convert a josn array to object array in JavaScript.


To accomplish the same we could get use of the $.map function



Below code snippet will convert  the json in jsondata variable to JavaScript array



You can see the result in the console screen of browser.

var o = [{"Rating":"Negative","Value":1.0},{"Rating":"Neutral","Value":1.0},{"Rating":"Postive","Value":3.0}];

var arr = $.map(o, function(el) { return [[el.Rating,el.Value]]; })

console.log(arr)

https://jsfiddle.net/92e03nqu/

Asp.net MVC how to populate dropdown list with numbers

Below Razor code will populate a dropdown with  numbers  from 1 to 10


@Html.DropDownListFor(=> m.BookRetensionDays, Enumerable.Range(1, 10).Select(rd => new SelectListItem { Text = rd.ToString(), Value = rd.ToString() }))