Pages

Sunday 25 November 2012

How to create your own code snippet in VS?

Code snippet is simply XML file which contains the code that we need to use most frequently while coding. I also like this feature very much because it save our developing time and increase productivity. You can create your own code snippet very easily. You can also add references and replacement parameters to your code.

For that first you need to install Snippet Designer Extension Tool in visual studio. You can also prepare snippet file(*.snippet) manually and add/import it using: Tool -> Code Snippets Manager (Ctrl + K, Ctrl + B). In this post i would like show you how to create and use snippet using Snippet Designer tool.

Steps to install Snippet Designer:
Goto Tools à Extension Manager.. à It opens Extension Manager window, Now you can install “Snippet Designer” from Online Gallery tab.


How to Create Snippet:
  • Just write your most usable code that you want to insert in snippet, just right click on code block and click Export as Snippet.



















  • Now you can set Snippet Name, Language and replacement setting.










How to Use Snippet:
  • Just press Ctrl + K, Ctrl + X in code behind file, it will open intellisense of all available snippet, so just go through snippet that you want. 





Saturday 24 November 2012

BringToFront() and SendToBack() in windows form.

When you work with multiple layer design means when controls goes to overwrite with each other in windows form then Bring to front and Send to back comes handy. You can use these in both design and code behind.

In Design:
Just right click on control that you want to move front or back.


















In Code Behind:
panelWait.BringToFront()
panelWait.SendToBack()

Sunday 18 November 2012

Solved : Cannot add a DataRelation or Constraint that has different Locale or CaseSensitive settings between its parent and child tables.

Many days ago, I was implementing parent-child relation in windows DataGridView using adding relation in DataSet among two DataColumns of two different DataTables and I faced below error.

Error Message :
Cannot add a DataRelation or Constraint that has different Locale or CaseSensitive settings between its parent and child tables.














Solution:
I found alternate solution from internet but below solution works for me fine. Its due to two datatable Locale property has different value. If you are going to add relationship then Locale of two datatable must be same. 

Mistake:
1) tblCustomer DataTable Properties:


2) tblOrder DataTable Properties:







Saturday 17 November 2012

How to show parent/child relation in windows DataGridView?

Today I am going to show you how to bind parent/child relation in windows form gridview. It’s very easy to show parent/child relation in grid using third party controls but I want to show relation without using additional controls. I found related help from internet but I like to show parent-child relation by adding DataSet relation on datacolums of DataTable.
C# Code:
       private void BindGridView()
        {
            // Create Tables
            DataTable tblCustomer = new DataTable("tblCustomer");
            DataTable tblOrder = new DataTable("tblOrder");

            // Create DataSet
            DataSet tblDataSet = new DataSet();

            // Create Columns and Add to Tables
            tblCustomer.Columns.Add("ID", typeof(int));
            tblCustomer.Columns.Add("CustomerName", typeof(string));
            tblOrder.Columns.Add("ID", typeof(int));
            tblOrder.Columns.Add("Order", typeof(string));
            tblOrder.Columns.Add("CustomerID", typeof(int));

            // Add Test Data
            tblCustomer.Rows.Add(1, "Bhaumik");
            tblCustomer.Rows.Add(2, "Ramesh");
            tblCustomer.Rows.Add(3, "Suresh");
            tblOrder.Rows.Add(1, "Order1.1", 1);
            tblOrder.Rows.Add(2, "Order1.2", 1);
            tblOrder.Rows.Add(3, "Order1.3", 1);
            tblOrder.Rows.Add(4, "Order2.1", 2);
            tblOrder.Rows.Add(5, "Order3.1", 3);
            tblOrder.Rows.Add(6, "Order3.2", 3);

            // Add Tables to DataSet
            tblDataSet.Tables.Add(tblCustomer);
            tblDataSet.Tables.Add(tblOrder);

            // Create Relation
            tblDataSet.Relations.Add("CustOrderRelation",
                tblCustomer.Columns["ID"], tblOrder.Columns["CustomerID"]);

            BindingSource bsCustomer = new BindingSource();
            bsCustomer.DataSource = tblDataSet;
            bsCustomer.DataMember = "tblCustomer";

            BindingSource bsOrder = new BindingSource();
            bsOrder.DataSource = bsCustomer;
            bsOrder.DataMember = "CustOrderRelation";

            // Bind Data to DataGridViews
            dgvCustomer.DataSource = bsCustomer;
            dgvOrder.DataSource = bsOrder;
        }

Demo:


Sunday 4 November 2012

Difference between And("&") and AndAlso("&&") Operator

And(“&”) Operator:
  Ø  It evaluates both sides even first condition goes wrong.
  Ø  It takes longer to execute for long expression.
  Ø  Example:
‘It throws exception.
If dt IsNot Nothing And dt.Rows.Count > 0
        ‘Bind Gridview Logic
End If

AndAlso(“&&”) Operator:
  Ø  It only evaluates the right side if the left side is true.
  Ø  Its faster compare to And operator.
  Ø  Example:
‘Works fine without exception.
If dt IsNot Nothing AndAlso dt.Rows.Count > 0
        ‘Bind Gridview Logic.
Else
        ‘No data found.
End If

Note: Same logic can apply for Or(“|”) and OrElse(“||”)