0% found this document useful (0 votes)
22 views3 pages

Program 23

The document describes a program for developing an ASP.NET application to connect to a database using ADO.NET for searching student data. It includes the source code for connecting to a database, adding, updating, deleting and searching student records.

Uploaded by

Aashish Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Program 23

The document describes a program for developing an ASP.NET application to connect to a database using ADO.NET for searching student data. It includes the source code for connecting to a database, adding, updating, deleting and searching student records.

Uploaded by

Aashish Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program 23- Develop an ASP.NET application for Connecting to the database with ADO.

NET
for Search Data.
Name-Aashish Pandey Date-15/04/2024
Source Code-
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" <div class="form-group">
CodeFile="Default.aspx.cs" Inherits="_Default" %> <asp:Button ID="Button1" runat="server"
<!DOCTYPE html> Text="Add Student" CssClass="btn btn-primary"
<html xmlns="http://www.w3.org/1999/xhtml"> OnClick="Button1_Click" />
<head id="Head1" runat="server"> <asp:Button ID="Button2" runat="server"
<title>Student Information</title> Text="Update Student" CssClass="btn btn-success"
<link OnClick="Button2_Click" />
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/c <asp:Button ID="Button3" runat="server"
ss/bootstrap.min.css" rel="stylesheet" /> Text="Delete Student" CssClass="btn btn-danger"
</head> OnClick="Button3_Click" />
<body> <asp:Button ID="Button4" runat="server"
<form id="form1" runat="server"> Text="Search Student" CssClass="btn btn-info"
<div class="container"> OnClick="Button4_Click" />
<h1>Student Information</h1> </div>
<div class="row"> <asp:Label ID="Label4" runat="server"
<div class="col-md-6"> CssClass="text-success"></asp:Label>
<div class="form-group"> </div>
<label for="txtRollNo">Roll <div class="col-md-6">
Number:</label> <asp:GridView ID="GridView1"
<asp:TextBox ID="TextBox1" runat="server" CssClass="table table-striped"
runat="server" AutoGenerateColumns="false">
CssClass="form-control"></asp:TextBox> <Columns>
</div> <asp:BoundField DataField="Roll_no"
<div class="form-group"> HeaderText="Roll Number" />
<label for="txtName">Name:</label> <asp:BoundField DataField="Name"
<asp:TextBox ID="TextBox2" HeaderText="Name" />
runat="server" <asp:BoundField
CssClass="form-control"></asp:TextBox> DataField="Phone_no" HeaderText="Phone Number" />
</div> </Columns>
<div class="form-group"> </asp:GridView>
<label for="txtPhoneNo">Phone </div>
Number:</label> </div>
<asp:TextBox ID="TextBox3" </div>
runat="server" </form>
CssClass="form-control"></asp:TextBox> </body>
</div> </html>
Default.aspx.cs-
using System; System.Data.OleDb.OleDbCommand cmd;
using System.Configuration; int i;
using System.Data;
using System.Data.OleDb; protected void Page_Load(object sender, EventArgs e)
using System.Linq; {
using System.Web; conn = new
using System.Web.Security; OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.
using System.Web.UI; 0;Data Source=D:\Aashish Pandey MCA sem 2\C#\
using System.Web.UI.HtmlControls; Practicals\program20\Student1.mdb");
using System.Web.UI.WebControls; da = new OleDbDataAdapter("select * from
using System.Web.UI.WebControls.WebParts; STUDENT1", conn);
using System.Xml.Linq; ds = new DataSet();
using System.Data; cb = new OleDbCommandBuilder(da);
using System.Data.OleDb; da.Fill(ds, "STUDENT1");
using System.Web.UI.WebControls; }

public partial class _Default : System.Web.UI.Page protected void Button1_Click(object sender,


{ EventArgs e)
System.Data.OleDb.OleDbConnection conn; {
DataSet ds; DataRow dr =
System.Data.OleDb.OleDbDataAdapter da; ds.Tables["STUDENT1"].NewRow();
System.Data.OleDb.OleDbCommandBuilder cb; dr[0] = TextBox1.Text;
DataView dv; dr[1] = TextBox2.Text;
dr[2] = TextBox3.Text; int Search = Convert.ToInt32(TextBox1.Text);
ds.Tables["STUDENT1"].Rows.Add(dr); OleDbDataAdapter daSearch = new
try OleDbDataAdapter("select * from STUDENT1 where
{ Roll_no=" + Search, conn);
da.Update(ds, "STUDENT1"); DataSet dsSearch = new DataSet();
ds.Clear(); try
da.Fill(ds, "STUDENT1"); {
GridView1.DataSource = daSearch.Fill(dsSearch, "STUDENT1");
ds.Tables["STUDENT1"]; if
GridView1.DataBind(); (dsSearch.Tables["STUDENT1"].Rows.Count > 0)
Label4.Text = "<br>Data Inserted Successfully!! {
</br>"; TextBox2.Text =
} dsSearch.Tables["STUDENT1"].Rows[0][1].ToString();
catch (Exception ex) TextBox3.Text =
{ dsSearch.Tables["STUDENT1"].Rows[0][2].ToString();
Label4.Text = "Error: " + ex.Message; Label4.Text = "<b> Data Found
} Successfully!!</b>";
} }
else
protected void Button2_Click(object sender, {
EventArgs e) Label4.Text = "No data found for the Roll
{ Number.";
OleDbCommand cmd = new }
OleDbCommand("Update STUDENT1 set Name='" + }
TextBox2.Text + "', Phone_no= '" + TextBox3.Text + "' catch (Exception ex)
where Roll_no=" + TextBox1.Text + "", conn); {
da.UpdateCommand = cmd; Label4.Text = "Error: " + ex.Message;
try }
{ }
conn.Open(); else
cmd.ExecuteNonQuery(); {
Label4.Text = "<br>Data Updated Successfully!! Label4.Text = "Please enter a Roll Number to
</br>"; search.";
} }
catch (Exception ex) }
{ }
Label4.Text = "Error: " + ex.Message; Output-
}
finally
{
conn.Close();
ds.Clear();
da.Fill(ds, "STUDENT1");
}
}

protected void Button3_Click(object sender,


EventArgs e)
{
ds.Tables["STUDENT1"].Rows[i].Delete();
try
{
da.Update(ds, "STUDENT1");
Label4.Text = "<br>Data Deleted Successfully!!
</br>";
}
catch (Exception ex)
{
Label4.Text = "Error: " + ex.Message;
}
}

protected void Button4_Click(object sender,


EventArgs e)
{
if (!string.IsNullOrEmpty(TextBox1.Text))
{

You might also like