Monday, 29 August 2016
How to apply Previous and Next result display using Asp.net MVC and Entity Framework
09:44
2 comments
Previous and Next result display using
Asp.net MVC and Entity Framework
Note:
here we will discuss
how to apply previous and next button for
apply this concept I have two Html.ActionLink() for (Previous
and Next).when I click on the next button it will show one by one customers
records from Sql server database .i used entity framework in MVC 4.
Here we have to use
Northwind database and we will apply this concept on Customers table ..........................
Step 1: First we
will take MvcApplication which named is WebApplication1
and create controller that’s named NoidaController
Code for NoidaController.cs.............................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using
WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class NoidaController : Controller
{
NORTHWNDEntities database = new NORTHWNDEntities();
private int pageItemNo = 12;
// GET: Noida
public ActionResult Index()
{
var v = database.Customers.OrderBy(m
=> m.ContactName).Take(pageItemNo).ToList();
TempData["total"] = database.Customers.Count() / pageItemNo;
ViewBag.nextPage = 1;
ViewBag.PrePage = 0;
return View(v);
}
// code for button(Previous and Next)
public ActionResult PreAndNext(int pageNo)
{
if (pageNo < 0)
{
pageNo = 0;
}
var count = Convert.ToInt32(TempData.Peek("total"));
var v = database.Customers.OrderBy(m
=> m.ContactName).Skip(pageNo * pageItemNo).Take(pageItemNo).ToList();
if (pageNo < count - 1)
ViewBag.nextPage = pageNo + 1;
else
ViewBag.nextPage = pageNo;
ViewBag.PrePage = pageNo - 1;
return View("Index",
v);
}
}
}
Code for Index.cshtml...............................
@model IEnumerable<WebApplication1.Models.Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ShowData</title>
<style type="text/css">
table {
border: solid 1px #DDEEEE;
border-collapse: collapse;
border-spacing: 0;
font: normal 13px Arial, sans-serif;
width: 200px;
height: 200px;
}
table thead th {
background-color: #DDEFEF;
border: solid 1px #DDEEEE;
color: #336B6B;
text-align: left;
text-shadow: 1px 1px 1px #fff;
}
table tbody td {
border: solid 1px #DDEEEE;
color: #333;
text-shadow: 1px 1px 1px #fff;
}
div {
margin: 5px;
border: 1px solid;
float: left;
}
.button {
display: inline-block;
outline: none;
cursor: pointer;
background-color: #ff6a00;
text-align: center;
text-decoration: none;
font: 14px/100% Arial, Helvetica, sans-serif;
padding: .5em 2em .55em;
text-shadow: 0 1px 1px rgba(0,0,0,.3);
-webkit-border-radius:
.5em;
-moz-border-radius: .5em;
border-radius: .5em;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.2);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.2);
box-shadow: 0 1px 2px rgba(0,0,0,.2);
}
.button:hover {
text-decoration: none;
background-color: aqua;
}
.button:active {
position: relative;
top: 1px;
}
</style>
</head>
<body>
<div>
@foreach (var item in Model)
{
<div>
<table>
<tr>
<th>CustomerId</th>
<td>@item.CustomerID</td>
</tr>
<tr>
<th>Name</th>
<td>@item.ContactName</td>
</tr>
<tr>
<th>Company Name</th>
<td>@item.CompanyName</td>
</tr>
<tr>
<th>City</th>
<td>@item.City</td>
</tr>
<tr>
<th>Country</th>
<td>@item.Country</td>
</tr>
<tr>
<th>Phone No</th>
<td>@item.Phone</td>
</tr>
</table>
</div>
}
</div>
<div style="clear:both;"></div><br />
<p>
@Html.ActionLink("Previous", "PreAndNext", new { pageNo =
ViewBag.PrePage }, new { @class = "button" })
@Html.ActionLink("Next>>", "PreAndNext", new { pageNo =
ViewBag.nextPage }, new { @class = "button" })
</p>
</body>
</html>
Result :
Friday, 5 August 2016
How to inserting multiple records in Table using Stored Procedures in Asp.net
03:33
No comments
Problem statement: If we want to insert multiple records in sql
table (if we click on 20, then 20 entries will be done)in BooksDetails table.
Means:
- we’ll insert multiple records here in the table as it is shown in this
problem that when we will increase the quantity of records then equal amount of
query will generate simultaneously and executes in the table that creates more
traffic that’s not acceptable .But this type of questions may be asked by
students.
Solution:
First
of all we can create table in SQL i.e BookDetails. In BookDetails table there
are 8 columns.
And in
ASP.Net we create web form.
Create table
BooksDetails
(
SrNo int identity(1,1),
BookType
varchar(50),
BookId as BookType + cast(SrNo as varchar(10)),
BookName
varchar(200),
Publisher
varchar(200),
WriterName
varchar(50),
Price
float,
BarCode
varchar(50)
)
Code
for Default.aspx ……………………………
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table align="center" border="1">
<tr><td>Name</td><td><asp:TextBox ID="Txtname" runat="server"></asp:TextBox></td></tr>
<tr><td>BookType</td><td><asp:TextBox ID="txtBookType" runat="server"></asp:TextBox></td></tr>
<tr><td>Writer</td><td><asp:TextBox ID="Txtwriter" runat="server"></asp:TextBox></td></tr>
<tr><td>Price</td><td><asp:TextBox ID="txtprice" runat="server"></asp:TextBox></td></tr>
<tr><td>Publisher</td><td><asp:TextBox ID="Txtpubli" runat="server"></asp:TextBox></td></tr>
<tr><td>Barcode</td><td><asp:TextBox ID="Txtbar" runat="server"></asp:TextBox></td></tr>
<tr><td>Quatity</td><td><asp:TextBox ID="txtquanty" runat="server"></asp:TextBox></td></tr>
<tr><td colspan="2"><asp:Button ID="Btnsubmit" runat="server" Text="Save" OnClick="Btnsubmit_Click" /><asp:Label ID="lblmessage" runat="server" Visible="false" Text="Label"></asp:Label></td></tr>
1
</table>
</div>
</form>
</body>
</html>
Code
for Default.aspx.cs ……………………………
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcon"].ConnectionString);
string res=null;
protected void Btnsubmit_Click(object sender, EventArgs e)
{
int qu = Convert.ToInt32(txtquanty.Text);
res = "insert into BooksDetails values";
for (int i = 1; i <= qu; i++)
{
// res += "insert into BooksDetails
values('"+txtBookType.Text+"','"+Txtname.Text+"','"+Txtpubli.Text+"','"+Txtwriter.Text+"',"+Convert.ToDouble(txtprice.Text)+",'"+Txtbar.Text+"')"
+ "\n";
res += "('" + txtBookType.Text + "','" +
Txtname.Text + "','" + Txtpubli.Text + "','" +
Txtwriter.Text + "'," + Convert.ToDouble(txtprice.Text)
+ ",'" + Txtbar.Text + "')" + ",";
}
SqlCommand cmd = new SqlCommand(res.Remove(res.Length
- 1), con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Books Saved Successfully";
}
}
// we are inserting multiple rows in a table. When we insert rows, too much data is uploading and leads to heavy traffic. This is not the correct way to insert multiple records at a time in a table.
So, here is a second method of inserting multiple
records in a table that is stored procedure.
Her we are creating stored procedures in SQL.
-- **************** stored procedures
**********************
create proc
MultipleBooksIns
@BookType varchar(50),
@BookName varchar(200),
@Publisher varchar(200),
@WriterName varchar(50),
@Price float,
@BarCode varchar(50),
@Quantity int
as
begin
declare @Counter int
set @Counter=0
While(@Counter<@Quantity)
Begin
insert
into BooksDetails values(@BookType,@BookName,@Publisher,@WriterName,@Price,@BarCode)
set
@Counter=@Counter+1
End
end
-- **************** end **********************
//Coding for multiple records in table.We create SQLConnection
in Web.Config file and call stored procedure in our Code. Through this way less
traffic occurs and integrity will be maintained .It reduces complexity also.
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcon"].ConnectionString);
string res=null;
protected void Btnsubmit_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("MultipleBooksIns", con);
cmd.Parameters.AddWithValue("@BookType",
txtBookType.Text);
cmd.Parameters.AddWithValue("@BookName",
Txtname.Text);
cmd.Parameters.AddWithValue("@Publisher",
Txtpubli.Text);
cmd.Parameters.AddWithValue("@WriterName",
Txtwriter.Text);
cmd.Parameters.AddWithValue("@Price", Convert.ToDouble(txtprice.Text));
cmd.Parameters.AddWithValue("@BarCode",
Txtbar.Text);
cmd.Parameters.AddWithValue("@Quantity", Convert.ToInt32(txtquanty.Text));
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
lblmessage.Visible = true;
lblmessage.ForeColor = Color.Green;
lblmessage.Text = "Books Saved Successfully";
}
Subscribe to:
Posts (Atom)