Monday, 23 May 2016

Creating a Connection String with SQL Server LocalDB

The MovieDBContext class you created handles the task of  connecting to the database and mapping Movie     objects to database  records. One question you might ask, though, is how to specify which database it will         connect  to. You don't actually have to specify which database to use,  Entity Framework will default to using      LocalDB. Here add  a connection string in the Web.config file of the application.

SQL Server Express LocalDB

LocalDB is a lightweight version of the SQL Server Express Database Engine that starts on demand and runs in user mode. LocalDB runs in a special execution mode of SQL Server Express that enables you to  work with            databases as .mdf files. Typically, LocalDB database files  are kept in the App_Data folder of a web project.
SQL Server Express is not recommended for use in production web applications. LocalDB in particular should not be used for production with a web application because it is not designed to work with IIS. However, a LocalDB   database can be easily migrated to SQL Server or SQL Azure.
In Visual Studio 2013 (and in 2012), LocalDB is installed by default with Visual Studio.
By default, the Entity Framework looks for a connection string named the same as the object context class         (MovieDBContext for this project).  For more  information see SQL Server Connection String for ASP.NET         web application.
Open the application root Web.config file shown below. (Not the Web.config file in the Views folder.)
Find  the <connectionStrings>  element:
Add the following connection string to the <connectionStrings>  element in the Web.config file.
<add name="MovieDBContext" 
   connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" 
   providerName="System.Data.SqlClient" 
/> 
The following example shows a portion of the Web.config file with  the new connection string added:
<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="MovieDBContext"    connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"
/>
The two connection strings are very similar. The first connection string is  named DefaultConnection and is used for the membership database to control who can access the application. The connection string you've added specifies a LocalDB database named Movie.mdflocated in the App_Data  folder.  We won't use the membership  database in this tutorial, for more information on membership, authentication and security.
The name of the connection string must match the name of the DBContext class.
using System;
using System.Data.Entity;

namespace MvcMovie.Models
{
    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }
    }

    public class MovieDBContext : DbContext
    {
        public DbSet<Movie> Movies { get; set; }
    }
}

Friday, 20 May 2016

Call back functions in ASP.Net MVC using Jquery


HomeController 

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ScriptLearning.Controllers
{
public class HomeController : Controller
{   
// GET: Home     
public ActionResult Index()
{
            return View();
        }
     }
}



View

@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<button class="buttonId">Click me</button><br /><br />
<div class="div1"> </div>
<style>
.div1 {
margin : 10px;
background: orange;
width :100px;
height:30px;
border: grey 2px solid;
}
</style>





Client.js 


$(document).ready(function () {
    $('.buttonId').click(function () {
        $('.div1').animate({ width: '400px', height: '400px' }, 1000, (function () {
            $(this).fadeOut(2000, (function () {
                $(this).fadeIn(1000, (function () {
                    alert(' Thanks for your time');
                }));
            }));
       
}));
   
});
});

Remove text/paragraph on clicking the button in Asp.net MVC using Jquery


HomeController 

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ScriptLearning.Controllers
{
public class HomeController : Controller
{   
// GET: Home     
public ActionResult Index()
{
            return View();
        }
     }
}



View


@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<button class="buttonId">Click to Hide</button>
<button class="button2">Click to Show</button>
<button class="button_toggle">Click to Toggle</button>
<br/> <br/>
<div class="text">This is the text which will remove on clicking about button </div>
<script src="~/Scripts/Client.js"></script>





Client.js 



$(document).ready(function () {
$('.buttonId').click(function () {
$('.text').hide();
});

$('.button2').click(function () {
$('.text').show();
});
$('.button2').click(function () {
$('.text').toggle();
});
});