Tuesday, May 20, 2014

Sql server row version data type in .net application

Today I'm going to discuss one of the most critical problems but which is not being focused by most of developers.

Here this is the scenario: let’s say we have a Leave Management system, employee has requested a leave and it goes to approval stage. Every manager can see all leaves, and two managers has loaded the same request REQ1 in there computer. What will happen if both managers approved the same request or done some different option (OR something different). It means we have to check the request states (weather it is already approved) before saving it.

 Solution 1:
We have to check the records before saving it. This approach is good but has to be done with lot of data checking with the data base. (OR have to do lot of data checking with the database)

Solution 2: (suggested)
In sql server there is a data type called timespan or row version (sql server 2008). Here is the link about the row version row version
The real advantage is, when   we made an update to the table the row version column changes its status to another, just like its status is updated to next sequence. In development we can flag the status easily. But the problem is that it's not in readable format e.g. .0x0000000000018F17.we have to convert this value to bit array in c#. As an alternative for that we can cast row version into sql server big int.
Here is the example

Scripts:
Create a database name test and run below scripts
create TABLE [dbo].[Requests](
 [ReqID] [int] IDENTITY(1,1) NOT NULL,
 [EmpID] [int] NOT NULL,
 [IsApproved] [bit] NULL,
 [Remarks] [varchar](50) NULL,
 [rv] [timestamp] NOT NULL,
 )
insert into Requests values(10,null,'Remarks 1',null)

select * from Requests

The RV column is the row version and sees the results. Change the employee table and see the results.
select *, cast(rv as bigint) from Requests
Run the above select and see the different. We can get the bits of array into big-int data type.

In Requests table ReqID is the primary key and rv value also needs to be checked for the changes. Here is the sample code for validating the changes.
Here is the completed project 

happy coding

Monday, May 19, 2014

List of Custom Objects Bind to combobox or datagridview in single method in .net

hi all,
This is my first post regarding the .net to share my experience with you all guys.
I've faced much inconvenience when
binding a combo box in net (Windows app), because we have to write more lines to complete the task.
Eg.
            Combobox1.DataSource =GetAllEmployees();
            Combobox1.DisplayMember = "EmpName";
            Combobox1.ValueMember = "EmpID";
Suppose GetAllEmployees() is a method that returns all list of employees. This is a typical binding code. In production environment systems are developed in some kind of architecture. In this example I’m using layered architecture to avoid the complexity of the application (Never develop application in single project because earlier it's easy for maintenance (OR to maintain) but day by day it will increase the complexity and then it will be nightmare to maintain.)          

How cool if we can convert above 3 lines to single line like this,

ComboBox1.EasyBind<Employee>(GetAllEmployees(), CmbSelection.Please_Select,"EmpID", "EmpName");
OR
ComboBox1.EasyBind<Employee>(GetAllEmployees(), "EmpID", "EmpName");


We can create user defined extension methods in .net to use like predefined methods like tostring() etc ,

EasyBind is .net customized extension method which is used to customize the binding.  if we have a list of employees, it will be bound to the combo box. CmbSelection is enum to attach the default selection (Please_Select).
Appreciate any comments or issues regarding this post.
Happy coding.