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 ๐Ÿ™‚

Advertisements

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

Bind Enum data type in dropdown list

February 10, 2014

Hello,

Here is a code snippet for bind enum data type in drop down list with datasource property.


var enumerationType = typeof(Entity.ProgramType);
 var dictionary = new Dictionary<int, string>();

foreach (int value in Enum.GetValues(enumerationType))
 {
 var name = Enum.GetName(enumerationType, value);
 dictionary.Add(value, name);
 }
 ddlProgramType.DataSource = dictionary;
 ddlProgramType.DataTextField = "value";
 ddlProgramType.DataValueField = "key";
 ddlProgramType.DataBind();
 ddlProgramType.Items.Insert(0, new ListItem("---Both---", "0"));

you will get your dropdown binded with enum ๐Ÿ™‚


Split string with multi-delimiter with asp.net c#

February 10, 2014

Hello,

Here is a trick for split string by another string

Or we can call split string by multi delimiter.


string data = "THExxQUICKxxBROWNxxFOX";

return data.Split(new string[] { "xx" }, StringSplitOptions.None);

this code returns you array by split main string with “xx”.


Send SMS using Twilio REST API

October 4, 2013

Here are some helpful notes for send sms using Twilio sms API.
First you should have account on http://twilio.com/
After creating a account Go to your visual studio and
install twilio package from Nuget package manager.
Use this code to send SMS,


protected Twilio.SMSMessage SendSMS()
{
string AccountSid = "your Sid get from https://www.twilio.com/user/account/settings";
string AuthToken = "get token from https://www.twilio.com/user/account/settings";
var twilio = new TwilioRestClient(AccountSid, AuthToken);

var message = twilio.SendSmsMessage("<strong>sender num</strong>", "<strong>reciever number</strong>", "<strong>your message body here</strong>");
//Note :: sender number you will get from here https://www.twilio.com/user/account/phone-numbers/incoming
try
{
if (message != null)
{
lblMessage.Text = message.Status;
if (message.Sid != null)
{
lblMessage.Text = lblMessage.Text + " SID: " + message.Sid;
}
else
{
lblMessage.Text = "Error";
}
}
else
{
throw new Exception("Error");
}
}
catch (Exception e)
{
lblMessage.Text = e.Message.ToString();
}
return message;
}

You will get status and SID of message.
you will get more info about API from https://www.twilio.com/docs


Datatable to Excel file without using a loop

October 2, 2013

Hello,

Using below function you canย  get excel file from you datatable ,as yes without using any for or foreach loop


public void ExportToExcel(DataTable dt, string filename)
{
HttpResponse response = HttpContext.Current.Response;

// first let's clean up the response.object
response.Clear();
response.Charset = "";

// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
// response.ContentType = "application/vnd.xls";
response.AddHeader("Content-Disposition", "attachment;filename=TestingReports.xls");

// create a string writer
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// instantiate a datagrid
DataGrid dg = new DataGrid();
dg.DataSource = dt;
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}