C# code of connectionless approach

Harshpinder
Posted by Harshpinder under ASP.NET category on | Points: 40 | Views : 3327
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection("data source=111;user id=111;password=111;database=111");

protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{

//CONNECTION LESS APPROACH on Page load
//SqlDataAdapter adapt = new SqlDataAdapter("select * from student", conn);
// DataSet ds = new DataSet();
//adapt.Fill(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();
//Response.Write("Student name is " + ds.Tables[0].Rows[1]["Name"].ToString());
}

}

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

}
protected void btnSearch_Click(object sender, EventArgs e)
{
//CONNECTION LESS APPROACH on button click
SqlDataAdapter adapt = new SqlDataAdapter("select * from student", conn);
DataSet ds = new DataSet();
adapt.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
code of default.aspx take an empty asp website
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>assignment2</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center"
style="background-color: #C0C0C0; font-family: 'Times New Roman', Times, serif; font-size: large; font-weight: bold; font-style: italic; color: #800000">

CONNECTION LESS APPROACH</div>
<center><asp:TextBox ID="txtsearch" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>&nbsp;
<asp:Button ID="btnSearch" runat="server" Height="26px" Text="Search"
Width="75px" onclick="btnSearch_Click" />
<br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" onselectedindexchanged="GridView1_SelectedIndexChanged">
<RowStyle BackColor="#E3EAEB" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView></center>

</form>
</body>
</html>

Comments or Responses

Login to post response