Tuesday 14 May 2013

Working DetailsView control in ASP.Net Ado.net way C#


DetailsView control in ASP.Net Ado.net way C#


Hello there, In this article i m using Detailsview control with simple way as we know that  the DetailsView control in ASP.Net 2.0 is used to create an HTML table that displays the contents of a single database record.
Now, create a database following the script you can copy and paste it in your sql server 2005 database
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id =OBJECT_ID(N‘[dbo].[tbformview]‘) AND type in (N‘U’))
BEGIN
CREATE TABLE [dbo].[tbformview](
[id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[Class] [varchar](50) NULL,
[Address] [varchar](50) NULL
) ON [PRIMARY]
END


Note* set the Allowpaging=”True


And now set event for Pageindexingchanging.
Now add a page with the name Detailview.aspx and copy paste the following source code in it.
<%@ Page Language=”C#” AutoEventWireup=”true”CodeFile=”Detailview.aspx.cs” Inherits=”Detailview” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>Untitled Page</title>
<style type=”text/css”>
.style1
{
width32%;
height151px;
}
.style2
{
width85px;
}
</style>
</head>
<body>
<form id=”form1″ runat=”server”>
<table class=”style1″>
<tr>
<td class=”style2″>
Name</td>
<td>
<asp:TextBox ID=”Txt_Name” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class=”style2″>
Class</td>
<td>
<asp:TextBox ID=”Txt_Class” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class=”style2″>
Address</td>
<td>
<asp:TextBox ID=”Txt_Address” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class=”style2″>
&nbsp;</td>
<td>
<asp:Button ID=”Bt_Submit” runat=”server” Height=”37px”
onclick=”Bt_Submit_Click” Text=”Submit” Width=”97px” />
</td>
</tr>
<tr>
<td class=”style2″>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td class=”style2″>
&nbsp;</td>
<td>
<asp:DetailsView ID=”DetailsView1″ runat=”server” AllowPaging=”True”
AutoGenerateRows=”False” Height=”63px”
onpageindexchanging=”DetailsView1_PageIndexChanging” Width=”167px”>
<Fields>
<asp:BoundField DataField=”Name” HeaderText=”Name” />
<asp:BoundField DataField=”Class” HeaderText=”Class” />
<asp:BoundField DataField=”Address” HeaderText=”Address” />
</Fields>
</asp:DetailsView>
</td>
</tr>
<tr>
<td class=”style2″>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
</table>
<div>
</div>
</form>
</body>
</html>
Now open the Detailview.aspx.cs file and paste the following code in it.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class Detailview : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlCommand cmd;
SqlDataAdapter adp;
DataTable dt;

protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString =ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
con.Open();
if (con.State==ConnectionState.Closed )
{
con.Open();
}
if (IsPostBack ==false)
{
detailvw();    // will call the detailvw() method in ispostback
}}
private void detailvw() // will fetch the record from database and display in GridView Control
{
if (con.State==ConnectionState.Closed )
{
con.Open();
}
adp = new SqlDataAdapter(“Select * from tbformview”, con);
dt = new DataTable();
adp.Fill(dt);
adp.Dispose();
if (dt.Rows.Count==0)
{
Response.Write(“No Record Found”);
}
else
{
DetailsView1.DataSource = dt;
DetailsView1.DataBind();
}
con.Close();
}
protected void Bt_Submit_Click(object sender, EventArgs e)//inserting the records in database.
{
if (con.State==ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand(“insert into tbformview values(@Name,@Class,@Address)”, con);
cmd.Parameters.AddWithValue(“@Name”, Txt_Name.Text);
cmd.Parameters.AddWithValue(“@Class”, Txt_Class.Text);
cmd.Parameters.AddWithValue(“@Address”, Txt_Address.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
detailvw();
con.Close();
}
protected void DetailsView1_PageIndexChanging(object sender,DetailsViewPageEventArgs e) // for paging the detailsview
{
DetailsView1.PageIndex = e.NewPageIndex;
detailvw();
}}


No comments:

Post a Comment