Get a list of month & year for particular duration

May 11, 2017

Here i have requirement to display month,year wise record count from table.The condition is starting month,year  would be month & year of Min start date in table and end month,year would be current month & year.

So here a query to fulfill the above requirement.

SELECT @FromDate=MIN(CreatedDate) FROM dbo.U***

DECLARE @MonthsAndYears table (Month INT, Year int)

;WITH dates AS
    SELECT @FromDate 'date'
        UNION ALL
        FROM dates t
    WHERE DATEADD(dd, 1, <= @ToDate
INSERT INTO @MonthsAndYears
    DATEPART(MONTH, date),
    DATEPART(YEAR, date)
FROM dates
option (maxrecursion 0)

SELECT *,CONCAT(DATENAME(MONTH,DATEADD(MONTH,  [Month] - 1, 0)),',',[Year]) AS Duration FROM @MonthsAndYears

Result is :


By using outer apply with your table on column created date (month & year part) to month and year column in query result ,we can get the desired result.

Get multiple result set from stored proc using Entity Framework

October 30, 2015

Hello folks,

I have some strange requirement.I want multiple result set from stored procedure.It does not seems a tough task but it’s taken few minutes to search out on google ,’cause i need this one in entity framework.So here i’m sharing the code snippet for the same.

For this we need to create wrapper class .Wrapper class is nothing but a simple class which wraps other types of class in single object.Seems quite confusing.Let’s go through example.

For example you have class named MyClass1,MyClass2,MyClass3 ,and you have stored proc which is returns these three types of result set.

So wrapper class would be,

public class WrapperClass
 public MyClass1 class1 { get; set; }
 public List<MyClass2> class2 { get; set; }
 public MyClass2 class2 { get; set; }

Our wrapper class ready now.

So here is a method to get result set from stored procedure.

public static WrapperClass GetMultipleResultSet(int Id)
 WrapperClass wrapper = new WrapperClass();

 using (var context = new myDBContext())
 context.Database.Initialize(force: false);
 var cmd = context.Database.Connection.CreateCommand();
 cmd.CommandText = "[dbo].[YourStoredProcedureName] @Id";
 var param = cmd.CreateParameter();
 param.ParameterName = "@Id";
 param.Value = Id;



 // Run the sproc 
 var reader = cmd.ExecuteReader();

 // Read first result set 
 wrapper.class1 = ((IObjectContextAdapter)context)


 wrapper.class2 = ((IObjectContextAdapter)context)
 wrapper.class3 = ((IObjectContextAdapter)context)
 return wrapper;

Happy Coding ūüôā

Get element from web browser control in windows application

June 5, 2015

I required to get element from web browser control and also wanted to set its value.

I searched out on google and got some solutions , so i’d like to share with you guys.

Here is a code snippet,

private void wbBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
WebBrowser wb = (WebBrowser)sender;

wb.Document.GetElementById("txtCity").SetAttribute("value", "myValue");
wb.Document.GetElementById("txtName").SetAttribute("value", "myValue");

Get Database name from Connectionstring

March 5, 2015

I have requirement to extract database name property from connection string.
Here is the code to get it,

string connStr = "Data Source=SERVERx;Initial Catalog=DBx;User ID=u;Password=p"; 

var csb = new SqlConnectionStringBuilder(connStr);

string dataSource = csb.DataSource;
string initialCatalog = csb.InitialCatalog; //Here you get database name

How to use web grid in mvc4

December 15, 2014

Here is a code snippet to use Webgrid in mvc

var grid = new WebGrid(ViewBag.List);
tableStyle: "webGrid",
headerStyle: "header",
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns
grid.Column("Name", header: "Name", format: m => m.Name),
grid.Column("City", header: "City", format: m => m.City),
grid.Column("",header: "Edit", format: @<text> @Html.ActionLink("Edit", "EditStudent", new  {id = item.Id }) </text>),
grid.Column("",header: "Delete", format: @<text>@Html.ActionLink("Delete", "DeleteStudent", new  {id = item.Id }, new { onclick = "javascript:return confirm('Are you sure want to delete this?')" } ) </text>)

Add a custom field for image in News widget

May 27, 2014

Add a custom field of type image to a news widget.
The following steps are given below

1. Add new custom field for News
2. Set its type to short text
3. Set “Interface widget for entering data” to Custom
4. In the custom textbox enter: Telerik.Sitefinity.Web.UI.Fields.ImageField
5. The image selector appears in the news publication view as expected and an image can be selected from local machine or image library

For more information check this, Add custom image field

Get choice field from dynamic module

May 27, 2014

Here is a simple way to get the list of possible values of a choice field of a dynamic module from code behind

Code snippet is given below.

var manager = ModuleBuilderManager.GetManager();
//Bind Choice field
var fieldList = manager.Provider.GetDynamicModuleFields().Where(f => f.Name == "YOUR_FIELD_NAME" && f.FieldNamespace == "YOUR_DYNAMICMODULE_NAMESPACE").Single();
var options =fieldList.Choices.Trim().Split(',');
TextReader txtReader = new StringReader(options[0]);
DataSet ds = new DataSet();
XmlReader reader = new XmlTextReader(txtReader);
//You will get list of choice field in xml
//And will convert it into DataTable
if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
    drpSelect.DataSource = ds.Tables[0];
    drpSelect.DataTextField = "text";
    drpSelect.DataValueField = "value";