Posts

Showing posts from 2013

Truncate All Tables in sql server database

USE [DatabaseName] EXEC sp_MSforeachtable 'TRUNCATE TABLE ?'   Be careful that this will delete ALL data from all user tables. In case you are not able to TRUNCATE due to foreign keys etc. you can run the same as a delete: USE [DatabaseName] EXEC sp_MSforeachtable 'DELETE FROM ?'

Difference Between Single, SingleOrDefault, First, FirstOrDefault in Linq

By using Single() you indicate you know only one item will be returned, so LINQ gives you exactly one item, and not a collection with only one item in it. var data = (from q in db.Items where q.Id == 1 select q).Single(); Where as SingleOrDefault() is useful if you're not sure if your query returns a record. When there isn't an item with an Id of 1, Single() will crash whereas SingleOrDefault() will return null when the item could not be found. var data = (from q in db.Items where q.Id == 1 select q).SingleOrDefault(); Note : While writing a query one should make sure that it should not return more that One Item else Not it will throw an Exception Exception : Sequence Contains more than One element. By Using First() you Indicate that you want  first element of the sequence Where as FirstOrDefault() is useful if you're not sure if your query returns any record. Summary FirstOrDefault() is for when zero or more results are expected to be present...

Browse Upload Image in Mvc3

Step 1 : Use Form tag In View with post method   @using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data",id ="parentForm"}))   {      <input type="file" name="file />      <input type="submit" name="submit" value="submit" /> }   Step 2 : In Action Controller use HttpPostedFileBase [HttpPost] public ActionResult Create(Images image,HttpPostedFileBase file) {      try     {          if (file != null)         {              if (file.ContentLength > 0)             {                  if ((file.ContentType == "image/jpeg") || (file.ContentType == "image/g...