Sunday, June 26, 2011

My blog post appearing on Channel9.msdn.com

Recently I was searching something and I found that one of the post about New Features of the SQL Server Denali was being discussed on Channel9 on MSDN. It’s a great pleasure and honour to have this kind of appreciation. I would like to thanks again Microsoft for giving opportunity and serve community. I would also like to thanks my readers who are giving me all the support and love.

Once again I am appealing good developers to start blogging. I have seen many great developers are who are doing great stuff but not blogging. I would like to say please please do the blogging and share your stuff with community and you will also get this kind of great appreciation.

Here is the link where they discussed that video.

https://channel9.msdn.com/Shows/This+Week+On+Channel+9/TWC9-Skype-HTML-5-Canvas-Python-tools--more

Following is video where Dan and Clint from Microsoft discussed my blog post on channel 9.



Here are the stuff they have discussed during the video.
Once again thank you for great appreciation. Stay tuned for more.. Happy programming.

Shout it
Share:

Code refactoring with Visual Studio 2010 Part-1

Visual studio 2010 is a Great IDE(Integrated Development Environment) and we all are using it in day by day for our coding purpose. There are many great features provided by Visual Studio 2010 and Today I am going to show one of great feature called for code refactoring. This feature is one of the most unappreciated features of Visual Studio 2010 as lots of people still not using that and doing stuff manfully. So to explain feature let’s create a simple console application which will print first name and last name like following.

ConsoleApplication

And following is code for that.

using System;

namespace CodeRefractoring
{
class Program
{
  static void Main(string[] args)
  {
      string firstName = "Jalpesh";
      string lastName = "Vadgama";

      Console.WriteLine(string.Format("FirstName:{0}",firstName));
      Console.WriteLine(string.Format("LastName:{0}", lastName));
      Console.ReadLine();
  }
}
}

So as you can see this is a very basic console application and let’s run it to see output.

Output

So now lets explore our first feature called extract method in visual studio you can also do that via refractor menu like following. Just select the code for which you want to extract method and then click refractor menu and then click extract method. Now I am selecting three lines of code and clicking on refactor –> Extract Method just like following.

ExtractMethod

Once you click menu a dialog box will appear like following.

ExtractMethodDialog

As you can I have highlighted two thing first is Method Name where I put Print as Method Name and another one Preview method signature where its smart enough to extract parameter also as We have just selected three lines with console.writeline. One you click ok it will extract the method and you code will be like this.

using System;

namespace CodeRefractoring
{
class Program
{
  static void Main(string[] args)
  {
      string firstName = "Jalpesh";
      string lastName = "Vadgama";

      Print(firstName, lastName);
  }

  private static void Print(string firstName, string lastName)
  {
      Console.WriteLine(string.Format("FirstName:{0}", firstName));
      Console.WriteLine(string.Format("LastName:{0}", lastName));
      Console.ReadLine();
  }
}
}
So as you can see in above code its has created a static method called Print and also passed parameter for as firstname and lastname. Isn’t that great!!!. It has also created static print method as I am calling it from static void main. Hope you liked it.. Stay tuned for more..Till that Happy programming.

Shout it

kick it on DotNetKicks.com
Share:
Friday, June 24, 2011

PetaPoco with parameterised stored procedure and Asp.Net MVC

I have been playing with Micro ORMs as this is very interesting things that are happening in developer communities and I already liked the concept of it. It’s tiny easy to use and can do performance tweaks. PetaPoco is also one of them I have written few blog post about this. In this blog post I have explained How we can use the PetaPoco with stored procedure which are having parameters. I am going to use same Customer table which I have used in my previous posts. For those who have not read my previous post following is the link for that.

Get started with ASP.NET MVC and PetaPoco
PetaPoco with stored procedures

Now our customer table is ready. So let’s Create a simple process which will fetch a single customer via CustomerId. Following is a code for that.

CREATE PROCEDURE mysp_GetCustomer
 @CustomerId as INT
AS
SELECT * FROM [dbo].Customer where  CustomerId=@CustomerId

Now we are ready with our stored procedures. Now lets create code in CustomerDB class to retrieve single customer like following.

using System.Collections.Generic;

namespace CodeSimplified.Models
{
  public class CustomerDB
  {
      public IEnumerable<Customer> GetCustomers()
      {
          var databaseContext = new PetaPoco.Database("MyConnectionString");
          databaseContext.EnableAutoSelect = false;
          return databaseContext.Query<Customer>("exec mysp_GetCustomers");

      }
      public Customer GetCustomer(int customerId)
      {
          var databaseContext = new PetaPoco.Database("MyConnectionString");
          databaseContext.EnableAutoSelect = false;
          var customer= databaseContext.SingleOrDefault<Customer>("exec mysp_GetCustomer @customerId",new {customerId});
          return customer;
      }
  }
}

Here in above code you can see that I have created a new method call GetCustomer which is having customerId as parameter and then I have written to code to use stored procedure which we have created to fetch customer Information. Here I have set EnableAutoSelect=false because I don’t want to create Select statement automatically I want to use my stored procedure for that. Now Our Customer DB class is ready and now lets create a ActionResult Detail in our controller like following

using System.Web.Mvc;

namespace CodeSimplified.Controllers
{
  public class HomeController : Controller
  {
      public ActionResult Index()
      {
          ViewBag.Message = "Welcome to ASP.NET MVC!";

          return View();
      }

      public ActionResult About()
      {
          return View();
      }

      public ActionResult Customer()
      {
          var customerDb = new Models.CustomerDB();
          return View(customerDb.GetCustomers());
      }
      public ActionResult Details(int id)
      {
          var customerDb = new Models.CustomerDB();
          return View(customerDb.GetCustomer(id));
      }
  }
}

Now Let’s create view based on that ActionResult Details method like following.

AddView

Now everything is ready let’s test it in browser. So lets first goto customer list like following.

CustomerList

Now I am clicking on details for first customer and Let’s see how we can use the stored procedure with parameter to fetch the customer details and below is the output.

CustomerDetails

So that’s it. It’s very easy. Hope you liked it. Stay tuned for more..Happy Programming.

Shout it

kick it on DotNetKicks.com
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