Friday, October 14, 2016

How To Set Textbox1 Value Into Formula Field Of Cystal Reports.


As per client requirement he/she want to selected date dispaly on report(Crystal Report).

Solution : 

Step1 : I have create one formula field "mindate" and "maxdate" in crystal report.

Step2 : Create the instance of the reports class and set the texbox value in crystalreportviwer source.

Example : 

I have used as per my code in aspx.cs in code page.

CrystalReportSource1.ReportDocument.DataDefinition.FormulaFields["mindate"].Text = "'" + Search_FromDate.Value.Trim() + "'";
CrystalReportSource1.ReportDocument.DataDefinition.FormulaFields["maxdate"].Text = "'" + Search_ToDate.Value.Trim()+ "'";
CrystalReportViewer1.ReportSource = CrystalReportSource1;

Note ["mindate"] ,["maxdate"] is the formula field name. which provided into crystal reports.

Result: See Below Image



Thursday, October 13, 2016

#C# Why Task introduced in C#?

Why Task introduced in C#?
.net framework provides System.Threading.Tasks.Task class to let you create threads and run them asynchronously. Queuing a work item to a thread pool is useful, but there is no way to know when the operation has finished and what the return value is.So that the reason Microsoft introduced the concept of Task.
Task is an object that represents some work that should be done.

Example:

public class Program {
    public static void Main() {
        // use an Action delegate and named method
        Task task1 = new Task(new Action(printMessage));
        // use an anonymous delegate
        Task task2 = new Task(delegate { printMessage() });
        // use a lambda expression and a named method
        Task task3 = new Task(() => printMessage());
        // use a lambda expression and an anonymous method
        Task task4 = new Task(() => { printMessage() });

        task1.Start();
        task2.Start();
        task3.Start();
        task4.Start();
        Console.WriteLine("Main method complete. Press <enter> to finish.");
        Console.ReadLine();
    }
    private static void printMessage() {
        Console.WriteLine("Hello, world!");
    }

}

Wednesday, October 12, 2016

#C# Ref Parameters vs. Out Parameters in C#


Ref Parameters vs. Out Parameters in C#


Ref: The ref keyword is used to pass an argument as a reference. This means that when value of that parameter is changed in the method, it gets reflected in the calling method. An argument that is passed using a ref keyword must be initialized in the calling method before it is passed to the called method.

Out: The out keyword is used to pass an argument as a reference. This means that when value of that parameter is changed in the method, it gets reflected in the calling method. An argument that is passed using a out keyword not to be initialized in the calling method before it is passed to the called method.

Before calling the method:
[ref]: The caller must set the value of the parameter before passing it to the called method as shown below example int I = 20;
[out]: The caller method is not required to set the value of the argument before calling the method. Most likely, you shouldn't. In fact, any current value is discarded as shown below example int K = 10;

During the call:
[ref]: The called method can read the argument at any time.
[out]: The called method must initialize the parameter before reading it.

Remote calls:
[ref]: The current value is marshaled to the remote call. Extra performance cost.
[out]: Nothing is passed to the remote call. Faster.

Example : class Program
    {
        public static void Main()
        {
            int i = 20;
            int k;
            Display(ref i);  //
            Display2(out k);
            Console.WriteLine("current ref value :" + i);
            Console.WriteLine("current out value :" + k);
            Console.ReadLine();
        }
        public static void Display(ref int i)
        {
            i = 10;
        }

        public static void Display2(out int i)
        {
            i = 10;
        }

    }
Output: Current ref value: 10
            Current out value: 10


Tuesday, October 11, 2016

#SQL What is the difference between a Clustered and Non Clustered Index?

What is the difference between a Clustered and Non Clustered Index?
  1.       One table can only have only one clustered index.
  2.       One table can only have many non-clustered indexes.
  3.       A clustered index requires no separate storage than the table storage. It forces the rows to be stored sorted on the index key.
  4.      A non-clustered index requires separate storage than the table storage to store the index information.
  5.      A table with a clustered index is called clustered table. Its rows are stored in a B-Tree structure sorted.
  6.      A table without any clustered indexes is called non-clustered table. Its rows are stored in heap structure unsorted.
  7.     The default index created as part of the primary key column is a clustered index
  8.     The default index created as part of the unique key column is a non-clustered index.
  9.       Prior to SQL Server 2008 only 249 Non-clustered Indexes can be created. With SQL Server 2008 and above 999 Non-clustered Indexes can be created

A table can have multiple non-clustered indexes
A table can have multiple non-clustered indexes because they don’t affect the order in which the rows are stored on disk like clustered indexes.
Why can a table have only one clustered index?
Because a clustered index determines the order in which the rows will be stored on disk, having more than one clustered index on one table is impossible. Imagine if we have two clustered indexes on a single table – which index would determine the order in which the rows will be stored? Since the rows of a table can only be sorted to follow just one index, having more than one clustered index is not allowed.


Monday, October 10, 2016

C# What is the difference between IEnumerable and IQueryable?

What is the difference between IEnumerable and IQueryable?


IEnumerable and IQueryable may seem like the same at a quick glance, but there are important differences that us, as developers must be aware so that we not only get a working product out the door but also something that performs well.



                      IEnumerable
                    IQueryable
·         IEnumerable exists in the System. Collections namespace.
·         IQueryable exists in the System.Linq Namespace.
·         IEnumerable is suitable for querying data from in-memory collections like List, Array and so on.
·          Example:  IEnumerable<Emp> emp = ent.Emps;
IEnumerable<Emp> temp = emp.Where(x => x.Empid ==5).ToList<Emp>();
This where filter is executed on the client side where the “IEnumerable” code is. In other words, all the data is fetched from the database and then at the client it scans and gets the record with “EmpId” is “5”.
·         IQueryable is suitable for querying data from out-memory like remote database collections.
·         Example : In this case, the filter is applied on the database using the “SQL” query. So the client sends a request and on the server side, a select query is fired on the database and only necessary data is returned.
·         IEnumerable not supported Lazy Loading . Hence not suitable for paging like scenarios.
·         IQueryable supported Lazy Loading .
·         While querying data from the database, IEnumerable executes "select query" on the server-side, loads data in-memory on the client-side and then filters the data.
·         While querying data from a database, IQueryable executes a "select query" on server-side with all filters.
·         IEnumerable is beneficial for LINQ to Object and LINQ to XML queries.
·         IQueryable is beneficial for LINQ to SQL queries.
·         IQueryable

Using Generics With C#

Using Generics With C#
Generics are most frequently used with collections and the methods that operate on them. Version 2.0 of the .NET Framework class library provides a new namespace, System.Collections.Generic, which contains several new generic-based collection classes.
Generics are different from generic collections. Generic collections are emerged from generic concept. It helps us to maximize code reuse, performance and type safety.
Generics are strongly type collections.
Generics help to decouple the data-type from the logical code snippet which makes the code to reuse again and again.
We can also create classes and pass to it the generic parameter as type.
We can use generics in our code by calling the namespace "using System.Collections.Generic;".
Microsoft applied generic concept on
.NET collections to make generic collections.
Arraylist to make list generic collections.
Hashtables to make Dictionary.
Stacks and Queues to make Stacks and Queues generics.

Example:   public class GenericsClass<T>
    {
   public bool Compare( T a , T b)
        {
            if(a.Equals( b))
            {
                return true;
            }
            return false;
        }    }
     }
    class Program
    {
    public static void Main()
    {
            GenericsClass<int> obj = new GenericsClass<int>();
            bool checkintData =  obj.Compare(10, 10);
            Console.WriteLine("Int data Check:{0}", checkintData);

            GenericsClass<string> obj1 = new GenericsClass<string>();
            bool StringData = obj1.Compare("indian", "indian");
            Console.WriteLine("String data Check:{0}", StringData);    }
    }
OutPut :  True, True
Advantages of Generic:

Type safety - this ensures that the method, class, or interface etc. works only on the correct data type. For example, if you use string when calling a generic method, then all the values passed in as expected to be strings. If you pass in an int value parameter, the compiler will detect that as an error and the program will not be able to compile.
Errors/Exceptions are caught at compile time as opposed to runtime errors/exceptions - if given a choice between compile time and runtime errors, 100% of developers choose compile time errors so should you. The last thing we want is errors crashing the program when the users are working with it.
Re-usability & Flexibility - in the above example on the problem statement, we declared more than one method and the only difference was the data type of the parameters that we passed. With generics, you only have to create one method and re-use it with any data type that you want

Write clean code - we do not need to write methods, class or interfaces that contain complex logic that checks the data type of the parameter values passed in.

Quickly Rename a Mysql database

How do I quickly rename a MySQL database (change schema name)?



From phpMyAdmin, select the database you want to select. In the tabs there's one called Operations, go to the rename section. That's all.
It does, as many suggested, create a new database with the new name, dump all tables of the old database into the new database and drop the old database.