The blog has moved to the new site F10Debug.com

Monday, September 25, 2017

How to assign default or null or empty parameter to Guid

One of my friend working on one project, and he wants to assign the null / Empty / default parameter to guid,

public void SampleFunction(Guid guidParameter = Guid.Empty)
{
}
But the compiler complains that Guid.Empty is not a compile time constant.
Solution 
We can use,
public void SampleFunction(Guid guidParameter = new Guid()){
  // when called without parameters this will be true
  var guidIsEmpty = guidParameter == Guid.Empty;}
When we call above method, without any paramter, then it will assign "Empty" Value to the guidParameter, so in that case "guidIsEmpty"  will be true.
We can also use default(Guid) instead of "new Guid()",  it will work same.

Why didn't Guid.Empty work?

The reason you are getting the error is because Empty is defined as:
public static readonly Guid Empty;
So, it is a variable, not a constant (defined as static readonly not as const). Compiler can only have compiler-known values as method parameters default values (not runtime-only-known).
Note:
Guid.Empty is equivalent to new Guid(), which is equivalent to default(Guid). So you can use:
Assign Guid.Empty value to Guid
public void SampleFunction(Guid guidParameter = default(Guid))
or
public void SampleFunction(Guid guidParameter = new Guid())
Assign Null value to Guid 
public void SampleFunction(Guid? guidParameter = null)



Saturday, September 16, 2017

Fiddler Wildcard AutoResponse For URL parameters

Suppose you want to enable autoresponder for perticular URL like,

https://dotnetpeoples.blogspot.in/Employee/Login?id=12345

but every time id changes when we re-fresh this page, so autoresponder not considering it,

so just do below 2 steps it will work for you guys,


Step 1:  Enable Automatic responses & unmatched request passthrough



Step 2: In Rule Editor Changed below URL as,

Exact:https://dotnetpeoples.blogspot.in/Employee/Login?id=12345  Changed to

REGEX: https://dotnetpeoples.blogspot.in/Employee/Login.*


Just replace querystring to .*

Thursday, January 5, 2017

How to set identity specification set to false globally in entity framework


We can globally turn off this feature by removing StoreGeneratedIdentityKeyConvention Convention as below,

public class OurContext : DbContext 
{    
protected override void OnModelCreating(DbModelBuilder modBuilder) {        modBuilder.Conventions.Remove<StoreGeneratedIdentityKeyConvention>();    
}
}

Thursday, December 22, 2016

microsoft visual studio can not set breakpoint in c# file

Microsoft visual studio can not set breakpoint in c# file

I was getting same error in one of my project, and after 15 to 20 mins of search & practical i found very simple solution as below,

BUILD > Clean Solution

BUILD > Build Solution

Hope it will help you guys.


Saturday, November 12, 2016

WCF InvalidOperationException: A binding instance has already been associated to listen URI

Here you missed the address attribute in your metadata endpoint.

A binding instance has already been associated to listen URI

Without it WCF thinks that you want to host the mex endpoint at the same address as other endpoints.




Sunday, June 26, 2016

Cannot insert the value NULL into column in ASP.NET MVC Entity Framework

Cannot insert the value NULL into column in ASP.NET MVC Entity Framework

One of my friend was trying simple MVC application [CRUD application] CRUD means 
C - Create, R - Read, U - Update & D- delete 

and he was getting error like "Cannot insert the value NULL into column in ASP.NET MVC Entity Framework" from almost more than half hour as below,

entity framework null value issue for id key

Here he was trying to save id & name into department table where id was not primary key of table.
Although he was specifying id value explicitly he was getting above error, because entity framework by default considers id as primary key and does not supply values to sql server.

Solution:

[Table("Department")]
    public class Department
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int id { get; set; }

        public string Name { get; set; }

    }

Here you just need to add one line above id property, to say entity framework that do not consider as database generated value.

If we want to set this option globally for every entities (for all tables), then we can do this by two ways as below,

- By setting identity specification set to false globally

  How to set identity specification set to false globally

- By creating custom conventions & add that custom conventions to DBContext class

  How to set DatabaseGeneratedOption Globally for all entities

- Using Fluent API

public class MyContext : DbContext {
    protected override void OnModelCreating(DbModelBuilder modBuilder) {
        modBuilder.Entity<MyEntity>()
                    .Property(e => e.Id)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    }
}

Monday, June 13, 2016

Delegates in C# with examples

Delegate is type safe function pointer.

Function pointer: points/holds referernce to a method.

Type safe:  return type and signature of method must be same as signature and return type of method else you will get compilation error.

Syntax:  Very much similar to the method with the delegate keyword.

It is similar to a class, we need to create instance of it and pass in the function name to the delegate constructor and this is the function the delegate will point to.

It calls the method indirectly via a pointer.

It is representative to communicate between two parties.

When you want to pass function as parameter to another funtion than think of delegates.

Main use of delegates in c# is Callbacks & communication between two parties.


Examples:


Example1:  Suppose we have report class and in that we have download report method. At real time you want to know the percentage of report downloaded, so in that case we can make use of delegates as below,

class Program
    {
        static void Main(string[] args)
        {
       // holds reference to function.
Report.DownloadReport(ReportDownloadStatus);
        }

        // callback function which you want to call using delegates.
        static void ReportDownloadStatus(int i)
        {
            Console.WriteLine(i);
        }
    }

    public class Report
    {
        //declare delegate
        public delegate void PercentageCompleted(int i);
       
        // delegate passed as a function parameter
        public static void DownloadReport(PercentageCompleted percentageCompleted) 
        {
            // Gathering and printing data
            for (int i = 0; i <= 100; i++)
            {
                percentageCompleted(i);
            }

            Console.WriteLine("Report Downloaded completly.");
        }
    }

Example2: Calling content page method from master page method / Click events.

Masterpage.cs

             protected void Button1_Click(object sender, EventArgs e)
             {
                 if (contentCallEvent != null)
                   contentCallEvent(this, EventArgs.Empty);
             }

            public event EventHandler contentCallEvent;

Contentpage.cs

protected void Page_PreInit(object sender, EventArgs e)
           {
               // Create an event handler for the master page's contentCallEvent event
               Master.contentCallEvent += new EventHandler(PrintData);
           }

           private void PrintData(object sender, EventArgs e)
           {
              // This Method will be Called.
              lbl.Text = "Delegates";
           }

ContentPage.ascx.cs

<%@ MasterType VirtualPath="~/Site.Master" %>

Dear Reader, please give suggestion on it, or let me know if you have good examples for delegates.