Friday, October 7, 2011

Using Stored Procedures in the Entity Framework with Scalar Return Values

This Link explains well on how to deal with stored procs returning scalar values using EF. Further it also explains how to handle if the proc returns null. If you have any doubt or need more examples, you can discuss with me here.

Friday, September 16, 2011

upload a file in MVC along with other components in your view


I am here going to describe how to upload a file in MVC along with other components in your view. The view is a content page.

let us take an example of an item object I will write here some portions of viewmodel, view page and controller function.


[Required(ErrorMessage = "Item Code Required")]
[StringLength(50, ErrorMessage = "Must be less than 51 characters")]
public string i_code

[Required(ErrorMessage = "Item Name Required")]
[StringLength(50, ErrorMessage = "Must be less than 51 characters")]
public string i_name

public HttpPostedFileBase Image



<% using (Html.BeginForm("Create","items",FormMethod.Post, new {enctype = "multipart/form-data"}))

<% =ViewData["Result"] %>
<%: Html.ValidationSummary(true)%>

<%: Html.LabelFor(model => model.i_code) %>
<%: Html.TextBoxFor(model => model.i_code) %>
<%: Html.ValidationMessageFor(model => model.i_code) %>

<%: Html.LabelFor(model => model.i_name) %>
<%: Html.TextBoxFor(model => model.i_name) %>
<%: Html.ValidationMessageFor(model => model.i_name) %>

<%: Html.LabelFor(model => model.FileUpload) %>
input type="file" id="Image" name="Image"

input type="submit" value="Create"

<% } %>'

Controller Function:

public ActionResult Create( itemviewmodel ToCreate)

if (ToCreate.Image.ContentLength > 0)
string filePath = Path.Combine(HttpContext.Server.MapPath("../bin/Uploads"),
Path.GetFileName(ToCreate.Image.FileName.Replace(' ','_')));
ToCreate.i_imgpath = filePath;



The things to be more careful are

1). Using a correct version of Html.BeginForm function(see above) and
2). Input type file should have "id" and "name" same value as the name of your viewmodel property field for file upload.
3). Make sure you have given sufficient permissions to the Network service or IUser Account for the folders where images will be stored, this is a common problem after deployment to many developers.

Further one more thing to be kept in mind is that your master page also has a
tag which will cause the
tag of the content page to be bypassed, to overcome this problem, remove
tag from the master page.

hope you will find this post helpful.
wish you good luck with file uploading.

Friday, March 25, 2011

calling a Stored Procedure using Entity Framework

Any stored procedure can be called through EF. Create a SP and add it to your model. Further go to the model browser, right click the sp, and click add function import, here select your SP, and select what type it returns, if it returns nothing then select 'None'. If it returns a sclar type then select the type, and if its returning a recordset then select complex. Click on get column information, it will show you the colums returning. click on create new complex type. and click ok. If its returning one of you entities then click on option entity and select the entity type from the list box.

now you call call it in your code.

if its returning a complex type use code:

MyEntities _entities = new MyEntities();
var p = from d in _entities.GetOrderInfo() select d;

if its returning nothing then use code:

MyEntities _entities = new MyEntities();
_entities.Cancel_Sale(ucode, oid);

Friday, February 25, 2011

simple application using mvccontrib grid

download the mvccontrib.dll, and add the reference of this dll to your application.

write a controller function:

public ActionResult salesreport()
handsetviewmodel vm = new handsetviewmodel();
return View(vm);


view code:

include the following namespace:

<%@ Import Namespace="MvcContrib.UI.Grid" %>
<%@ Import Namespace="MvcContrib.UI.Grid.ActionSyntax" %>

write the following code to display the grid

<%: Html.Grid(Model.handsetstock()).Columns(column => {

column.For(c => c.imeicsv);