Returning from finally

Can we return from finally block? If your answer is yes, take a look at this code.
We have a function body as ;

   return "From try"; 
catch (Exception ex)
   return "From Catch"; 
   return "From Finally"; 

If this is enclosed in a function and we are giving call to the function,
what will be the return value of this statement?
And the answer is, it will give a compilation error, as we cannot return from finally block.

Nested insert using LINQ

Consider database structure we have created in previous post. Both tables share a relationship.
Now if one wants to insert records in both the tables, 2 seperate queries are required to update each table.
LINQ provides a mechanism where tables are interdependent it manages the insertion of records. i.e. using LINQ one need to fire the insert query only once and that on parent table.

Database structure is;
Here is a code to illustrate that;

string employeename = txtEmployeeName.Text;
string deptName = txtDeptName.Text;

Employee emp = new Employee();
emp.EmployeeName = employeename;

Department dept = new Department();
dept.DeptName = deptName;


This will insert records in both the tables at one insert request and it will 
handle the responsibility of updating references.

How to use SQL metal to generate entity set

Let's start with database creation. Here I have created 2 tables as
       EmployeeID *
       DeptID (FK)

       DeptID *

Create a windows forms application using Visual studio ( I am using Visual Studio 2010). Let's say LINQSample.
Create a class to have entity set which we are going to generate through SQLMETAL command. I have created a class say LINQSample.cs.
Then create a sqlmetal command as;

cd "E:\MySamples\LINQSample\LINQSample"
sqlmetal /conn:"Integrated Security=SSPI;Persist Security Info=False;INITIAL CATALOG=MySampleDB;DATA SOURCE=(local)" 
/code:E:\MySamples\LINQSample\LINQSample\LINQSample.cs /sprocs /views /namespace:LINQSample

Here I have created project in above mentioned location with database name as MySampleDB. You can use any other names here.

Go to visual studio command prompt and run the above command. This will generate a class with database name. It consist of the entity set provided in database. It is a C# representation of the database where tables are mapped as individual classes which we can use in our code anywhere and perform database operations using c# code.

Populate combobox from Datatable

Sometimes a need comes to to populate a combobox in such a way that values displayed to user are different than the actual selection options. In this case a mapping is created via code like key-value pair.
Recently I was doing the same thing, when I tried to do the same using DataTable.

Here is a code sample which shows that how to make use of DataTable to populate combobox.

            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));

            dt.Rows.Add(1, "Batman");
            dt.Rows.Add(2, "Spiderman");
            dt.Rows.Add(2, "Iron man");
            dt.Rows.Add(2, "Megamind");
            dt.Rows.Add(2, "Bug");

            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "ID";

When you do so, combobox will show you the values specified in column "Name". But in the actual the ValueMember specified over here is column "ID".

If you check the SelectedValue property of the combobox, you will find the value from column "ID".
This technique has got many applications like showing user-friendly value to the user and use some other value for computations.

Are you planning for .Net certification exam ?

There are 45 questions to be attempted in atleast 2 hours. You must plan properly before appearing for the exam.
There is a 2nd edition of certification book is available which focuses on Visual Studio 2008. Earlier was focused on
Visual Studio 2005. This new edition also comes up with updates and corrections to the prior version.
This exam mainly focus on fundamentals of .Net framework so it is not at all required to know .net frameworks other than 2.0.
If you are working on 2.0 version it is perfectly fine.
The passing score is 700 out of 1000 or 70%.

To know more details kindly visit this link.