Showing posts with label ADO.NET. Show all posts
Showing posts with label ADO.NET. Show all posts
Friday, January 18, 2013

How to get N row from datatable in C#

Problem:


Recently one of my friend was needed only first three rows of data table and so he asked me and I have multiple solutions for that. So thought it would be great idea to share with you guys.

Possible Solutions to problem:


There are two ways to solve this problem.
  1. With normal for loop
  2. With lamda expression and linq

1. With normal for loop:

Following is code from where we can get 3 rows of data table.
Share:
Sunday, April 10, 2011

View Call Hierarchy window in Visual Studio 2010

Visual studio 2010 Ultimate is great IDE(Integrated Development Environment) and I am loving more and more whenever I found some thing new. While doing some Code and Review and other stuff with Visual Studio 2010 I have found one of most interesting feature in Visual Studio 2010 called “Called Hierarchy”.You can navigate your code better with the Call Hierarchy feature in Visual Studio 2010.
To check out let’s Create a sample asp.net MVC application like following .

CallHirerchay

After creating a ASP.NET MVC 2 application it will create a basic simple application and we can run it and We have two views like home and about. We will check few code with Call Hirarchy. We can enable Call hierarchy window with two way one from the View->View Call Hierarchy like following or you can also use the shortcut Ctrl+Alt+K.

CallHirerarchywindow 

Now lets check the Account Controller-> Logon Action Result with call hierarchy You can also enable call hierarchy with selecting the Logon action and right click –> View Call Hierarchy like following or You can use the Shortcut for that like Ctrl + K,Ctrl + T.

ViewCallHierarchy 

Once you click the call Hierarchy It will open the code that is called logon and also call made from the logon method like below.

CallHierarchayforLogin

One you expand the both ‘Call To LogOn’ and ‘Calls from LogOn’ you can see all the calls Since there is no call for Logon on asp.net mvc sample application it will say but you can see calls made from it like following.

DetailOfCall

And on double click on any method or property will navigate you that code. Like I have double clicked the IsNullOrEmpty I have navigated to string class with that property.

StringIsnullOrEmpty

That’s it. So you can see its a great way to navigate code for code review and code inspection. Hope you liked it.. Stay tuned for more.. Happy Programming..

Shout it

kick it on DotNetKicks.com
Share:
Sunday, January 9, 2011

Distinct operator in Linq

Linq operator provides great flexibility and easy way of coding. Let’s again take one more example of distinct operator. As name suggest it will find the distinct elements from IEnumerable. Let’s take an example of array in console application and then we will again print array to see is it working or not. Below is the code for that. In this application I have integer array which contains duplicate elements and then I will apply distinct operator to this and then I will print again result of distinct operators to actually see whether its working or not.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Experiment
{
class Program
{
static void Main(string[] args)
{
int[] intArray = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5 };
var uniqueIntegers = intArray.Distinct();
foreach (var uInteger in uniqueIntegers)
{
Console.WriteLine(uInteger);
}
Console.ReadKey();

}
}
}
Below is output as expected..
DisntictResult

That’s cool..Stay tuned for more.. Happy programming.
Technorati Tags: ,
Shout it
Share:
Saturday, November 14, 2009

C# 4.0 New feature - Named Parameter

C# 4.0 has one new cool features which is the named parameter. Suppose you have so many parameter in function and when you call them its hard to remember the sequence of the code now with the named parameter you can have name of the parameter with value like Parameter:value and one another cool feature of the name parameter is the you don't need to pass the parameter in exact sequence. Let see the following example



Code Snippet



  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. foreach (int i in Square( max:10,min:1))
  4. {
  5. Response.Write(i.ToString() + " ");
  6. }
  7. }

  8. public static IEnumerable<int> Square(int min, int max)
  9. {
  10. for (int i = min; i < max; i++)
  11. {
  12. yield return i*i;
  13. }
  14. }





In above example I am having a function called square with two parameter min and max and having a function which will return the square of the given range between min and max. See the for each loop carefully. I have named parameters and another thing you will notice that i have changed the sequence of parameter but still its working fine. So this is a very cool new feature from the C# 4.0.

Happy Programming...

Technorati Tags: ,

kick it on DotNetKicks.com
Shout it
Share:
Tuesday, October 27, 2009

Review of Microsoft Visual Web Developer 2010 beta 2 express edition.

Microsoft visual studio express editions are light weight IDE provided free by Microsoft and also having almost all the functionality of its big brother Microsoft Visual Studio Professional edition except few. My pc is bit old now Its P4 and its having only 1gigs of ram so the express edition are the best suited for pc like mine. Microsoft has recently provided the beta 2 version of Microsoft new generation IDE called Visual Studio Twenty Ten(2010).

There are lots of changes from visual studio 2008 express edition and Microsoft Visual Studio 2010 Express edition. The first changes you will notice is the brand new splash screen and brand new colored logo for visual studio.

LogoVisual Splash

Another thing you will notice is the look and fill of IDE. It all looks like blue every where. Its is best suited for the Microsoft forthcoming operating system Microsoft Windows 7.

VWD

Another the difference is the create new project window which is bit stylish and having more options compare the to the visual studio 2008. There are two new things in new project dialog the first one the windows azure tools which is Microsoft's new steps towards the could computing(I will blog about this later one). and another thing is the Silver light application option. Now you can create three kind of application with the Microsoft SilverLight Application

  1. Silverlight Application
  2. Silverlight Navigation Application
  3. Silverlight class library

OpenDialog

Another difference is the default font for the IDE earlier it was Courier New and now its Consolas which is the next generation programming font. Which is more visual than the other fonts.

Another major difference is when you create a asp.net web application then its now having more default folders compare to visual studio 2008. There are site.master is also there and some default style sheet just like asp.net mvc applications. You will have default login control like asp.net mvc application. There are two more folder scripts and styles. In style you will have default style sheet for the application. Another folder is for the scripts which are having default JQuery as per earlier announcement Microsoft and JQuery will go together.

SolutionsExplorer

Another new things in Microsoft Visual Studio 2010 Visual Web Developer Expression edition is the Tool Setting menu which are having three setting by default.

  1. Basic Settings
  2. Code Optimized
  3. Expert Settings.

All the three option will have different setting for solution explorer and all other stuff. Another new thing is visual web developer 2010 express edition having multi monitor support which is not there in earlier express editions.Visual web developer express also having windows presentation foundation code editor.

Another cool feature is the maximized windows in design view now you will have whole window is maximized in design view hiding other windows. So you visually look all the control more better. Another cool thing is the percentage dropdown through which you can set all screen by percentage as per your requirement.

There are lots of more features like Improved css compatibility,HTML and Javascript snippets,support for asp.net mvc application and support for multi target etc. I will blog about each feature in much more detail. T

If you still not downloaded the future version of visual studio express edition 2010 then you can download it from the following link.

http://www.microsoft.com/express/future/default.aspx

Shout it
Share:
Saturday, July 25, 2009

Sending mail with secure service layer in asp.net 3.5/2.0

In earlier post i have mentioned that how to send the email using new System.Net.Smtpmail with asp.net 2.0 or higher version. Now lets learn how to send the mail with the secure service layer in the asp.net.

Security is the one of the most important requirement in today’s world as you are doing your business online you have keep secure from other wrong elements. Secure service layer add an

extra layer of security to your web application and sending mail with the SSL with tighten your security for mails. Your emails are more secure then ever and your valuable information will not going to wrong hands.

Here is the code from which we can send the email with the secure service layer. As i have mentioned in earlier post you need to to send email with authentication to use SSL. So you need to write following code. First you need to create message like following..

MailAddress fromAddress = new MailAddress("[email protected]","Nameofsendingperson");
message.From = @”fromAddress”;//here you can set address
message.To.Add("mailto:[email protected]%22%29;//");//
message.Subject = "Sending email with ssl";
message.CC.Add("[email protected]");//ccing the same email to other email address
message.Bcc.Add(new MailAddress("[email protected]"));//here you can add bcc address
message.IsBodyHtml = true;//To determine email body is html or not
message.Body =@"Plain or HTML Text";
Then you have to sent message with following code.
System.Net.Mail.SmtpClient mailClient =
 new System.Net.Mail.SmtpClient("your smptp","ssl port");
//This object stores the authentication values
System.Net.NetworkCredential basicCrenntial = 
new System.Net.NetworkCredential("username", "password");
mailClient.Host = "Host";
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicCrenntial;
// You have to add one other properties for SSL
mailClient.EnableSSL=true;
mailClient.Send(message);
Share:
Wednesday, June 10, 2009

What’s new in asp.net 4.0 and .net Framework 4.0?

Microsoft.NET Framework 4.0 beta version is out now. Let’s see the what’s new in asp.net 4.0 for the developer purpose. There are lots of features are there. Some of them are following.

1) Extensible Output Caching:

ASP.NET 4.0 adds an extensibility to output caching now you can create your cache provider and will can use this provider for the caching. So now you can use your own data source for you caching.

2) AutoStart Webapplication

Before asp.net 4.0 you have to use application_load or application_start event for the extensive data processing before application pages loads. But now you can use auto start features of asp.net 4.0. A new scalability feature named auto-start that directly addresses this scenario is available when ASP.NET 4.0 runs on IIS 7.5 on Windows Server 2008 R2. The auto-start feature provides a controlled approach for starting up an application pool, initializing an ASP.NET application, and then accepting HTTP requests.

3) Permanently Redirecting a page

Before asp.net 4.0 you need to use response.redirect method to go to another page from new page. ASP.NET, developers have traditionally handled requests to old URLs by using by using the Response.Redirect method to forward a request to the new URL. However, the Redirect method issues an HTTP 302 Found (temporary redirect) response, which results in an extra HTTP round trip when users attempt to access the old URLs. Now permanent redirect will solve this. Its will also benefit for the Search Engine Optimization.

4) Compressed Session State:

ASP.NET 4.0 will have new option for session stored in sql server or in state service. You can compress your serialized session using GZip Compression

5) Page.Keywords and Page.Description Properties

In asp.net 4.0 now you can create dynamic keywords and description for a page using Page.Keywords and Page.Description Properties

6) Enabling View state for Individual Controls

In earlier version of the asp.net if you disable your parent control view state it will disable your child control view state but now with asp.net view state is not dependent on parent child control view state.

7) Routing in asp.net 4.0

Now you can have build in routing for your pages. You don’t have to write your url rewriting modules. It introduces a WebFormRouteHandler routing class which will take care of routing.

8) ClientID

In earlier asp.net version clientId is system generated and if you want to change you have to override it . ASP.NET by default generated a big string for the clientid which will increase your html kb. With asp.net you have small client id using different options Legacy,Static,Predictable,Inherit

There are several other features are also there like following

Live Data Binding

Observer pattern using JavaScript and JavaScript objects

ADONetServiceProxy Class

Formview Control Ehancements

Ajax Improvements in asp.net 4.0

I have just list some of the features you can have all the features available in asp.net 4.0 white paper from the following link.

http://www.asp.net/learn/whitepapers/aspnet40/

Cheers… Happy Programming…

Share:
Sunday, May 31, 2009

DataSet Vs Datareader

Before some time a reader  vamsi asked me in my increase asp.net application performance post that why should i prefer data reader over dataset. Both data reader and dataset are usually use to fetch data from the database but both are having different mechanism for fetching data from database. First we looked into that and then we decide in which condition we need to use dataset and in which condition we need to use data reader.

DataReader:

Datareader fetch data one by one from the database. You can have one row(record)  at a time in datareader. Once you read next row with read method of the datareader the older record is destroyed from memory and new record will take place instead of it. You can do that only forward only and you can not modify the data. It also require a database connection to stayed open while fetching data. You can use CommandBehavior.CloseConnection property to close connection after data reader finish reading data from database.

Dataset:

You can call dataset a mini database in your memory. Dataset support disconnected architecture as once you fill dataset with data then no connection is required. A database can contain multiple tables also its supports relationship between them. You can insert,update and delete records from dataset and you can update them also in database.

Where i should use dataset over database?

When you are required to display only data then use datareader.Dataset will fetch data once a time. So if you are having millions of records then it will consume your server memory while datareader will have just one record at a time in database. And you need not to display millions of records at time.

Following are the link from which you can find which is best suited for your need.

http://www.simple-talk.com/dotnet/.net-framework/should-you-use-ado.net-datareader-or-dataset/

http://aspnet.4guysfromrolla.com/articles/050405-1.aspx

http://www.netnewsgroups.net/group/microsoft.public.dotnet.framework.aspnet/topic740.aspx

Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews