Example11 .aspx
<%@ Page Language="C#" ErrorPage ="~/Error.aspx" AutoEventWireup="true" CodeFile="Example11.aspx.cs" Inherits="Example11" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
AutoGenerateSelectButton="true"
onselectedindexchanged="GridView1_SelectedIndexChanged">
</asp:GridView>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px">
</asp:DetailsView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using NLog;
public partial class Example11 : System.Web.UI.Page
{
SqlConnection connObj = new SqlConnection();
DataSet ds;
SqlCommand objCommand;
SqlDataAdapter objAdapter;
protected void Page_Load(object sender, EventArgs e)
{
connObj.ConnectionString = ConfigurationManager.ConnectionStrings["DotNetTrainingConnectionString"].ToString();
//Bind Data To grid View
connObj.Open();
ds = new DataSet();
objCommand = new SqlCommand("Select * from student", connObj);
objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(ds);
connObj.Close();
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanging(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
int id =int.Parse ( row.Cells[1].Text );
//Bind selected row from grid View to Details View connObj.Open();
ds = new DataSet();
objCommand = new SqlCommand("Select * from student where Student_ID= " + id, connObj);
objAdapter = new SqlDataAdapter(objCommand);
objAdapter.Fill(ds);
connObj.Close();
DetailsView1 .DataSource = ds.Tables[0];
DetailsView1.DataBind();
} }
Below is the Connection String in web.config file.
<add name="DotNetTrainingConnectionString" connectionString="Data Source=.\sqlexpress;Initial Catalog=DotNetTraining;Integrated Security=True" providerName="System.Data.SqlClient"/>
See the below o/p on 10 as selected row from the Grid
No comments:
Post a Comment