Archive

Archive for the ‘ASP.Net MVC’ Category

How to use Stored Procedures in Entity Framework

July 8th, 2010 Anuraj P 3 comments

In the current project we are using Entity Framework for database operations. Entity Framework comes with Visual Studio SP1, which helps you to map tables / views / procedures as entities in C# / VB Code. You can find more details about EF from here : http://msdn.microsoft.com/en-us/library/bb399572.aspx. In this post I am explaining how to use Stored Procedures in Entity Framework.

  1. Add the Stored Procedure to the Entity Model Designer using Update Model From Database Option.
  2. Add Procedure

    Add Procedure

  3. If you are added successfully, you can get the procedure in Model Browser.
  4. Model Browser

    Model Browser

  5. Right click on the Procedure name and select Create Function Import.
  6. Create Function Import

    Create Function Import

  7. It will popups a Windows with Stored Procedure Name, Function Import Name and Return Type. If the procedure returns nothing, you can choose none. If the procedure is returns single value, like UserId, Number Of Rows etc, then you can choose scalar option, where you need to specify the return type. And if the procedure is returns Table or Number of Rows, you need to choose the last option Entities, which will allow to select entities created in the Model as the Output. Sometimes we need to create a View in the DB and need to import it in the Model, so that we can use the View as the return type entity. Select the appropriate return type and click Ok. You can use this in code. In this code I am using a View to return the selected users.
Add Function Import dialog

Add Function Import dialog


using (SampleEntities context = new SampleEntities())
{
/*
* Thanks to Barry Soetoro.
* I was not calling the GetAllUsers function.
List<Users> Users = null;
Users = (from user in context.Users
             select user).ToList();
this.dataGridView1.DataSource = Users;
*/
//Updated Version.
IEnumerable<UsersView> userview = context.GetAllUsers();
this.dataGridView1.DataSource = userview;
}

This will display list of Users in the DataGridView. Happy Coding :)

Creating Captcha HTML Helper

September 15th, 2009 Anuraj P 2 comments

After working with ASP.Net MVC, and my previous posts, in this Post I am trying to implement a Captcha HTML Helper, an HTML Helper is just a method that returns a string. Creating Custom HTML Helpers. It is an extension method, to the existing HTML Helper class.
Here is code.(For more details about the Captcha generation code, check my previous post. : Captcha using ASP.Net and C#);

 public static class CaptchaHelper
    {
        public static string Captcha(this HtmlHelper helper, string text)
        {
            string srcPath = System.Web.VirtualPathUtility.ToAbsolute("~/Handler1.ashx");
            string htmlContent = string.Empty;
            htmlContent += "<script type=\"text/javascript\">function __rc(){document.getElementById(\"" + text +
                           "\").src = \"../Handler1.ashx?query=\" + Math.random();}</script>";
            htmlContent += string.Format("<img id=\"{0}\" src=\"{1}\" alt=\"Captcha Image\"/>", text, srcPath);
            htmlContent += "<a href=\"#\" onclick=\"javascript:__rc();\">Reset</a>";
            return htmlContent;
        }
    }

And in the View you can use like, you may need to import the Namespace of CaptchaHelper class.

 < %= Html.Captcha("Sample") %>

It will render a security image. Happy Programming