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;

 cmd.Parameters.Add(param);

 context.Database.Connection.Open(); 

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

 // Read first result set 
 wrapper.class1 = ((IObjectContextAdapter)context)
 .ObjectContext
 .Translate<MyClass1>(reader).FirstOrDefault();

 reader.NextResult();

 wrapper.class2 = ((IObjectContextAdapter)context)
 .ObjectContext
 .Translate<MyClass2>(reader).ToList();
 
 reader.NextResult();
 
 wrapper.class3 = ((IObjectContextAdapter)context)
 .ObjectContext
 .Translate<MyClass3>(reader).FirstOrDefault();
 
 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");
wb.Document.GetElementById("btnSave").InvokeMember("click");
}

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

<pre>@{
var grid = new WebGrid(ViewBag.List);
@grid.GetHtml(
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);
ds.ReadXml(reader);
//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";
    drpSelect.DataBind();
}       

Bind Categories in dropdownlist with sitefinity

May 27, 2014

Programmatically populate DropDownList with Taxonomy Categories,

In the sitefinity sometime you need to bind collection list/Categories into repeater grid or drop down.

Below code snippet will helps you for it

TaxonomyManager manager = TaxonomyManager.GetManager();
var taxonomy = manager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Categories").SingleOrDefault();
var categoryList = taxonomy.Taxa.ToList();
drpEventTypes.DataSource = categoryList;
drpEventTypes.DataTextField = "Title";
drpEventTypes.DataValueField = "Id";
drpEventTypes.DataBind();