Practical No: 6B Aim: Create A Web Application To Display Records by Using Database

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

PRACTICAL NO: 6B

Aim: Create a web application to display records by using database.

Note: only write code and output.


Code:

1] WebForm1.aspx

<div>
<asp:TextBox ID="TextBox1" runat="server"
AutoPostBack="True"></asp:TextBox>
&nbsp;<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Button" />
&nbsp;
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:EMPLOYEEConnectionString2
%>" SelectCommand="SELECT [Id], [name], [salary] FROM
[emp]"></asp:SqlDataSource>
</div>

2] WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
namespace prac6B
{
public partial class WebForm2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=DESKTOP-
123\\SQLEXPRESS;Initial Catalog=EMPLOYEE;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
cmd.Connection = con;
}

protected void Button1_Click(object sender, EventArgs e)


{
cmd.CommandText = "select name from emp where id='" +
TextBox1.Text + "';";
Label1.Text = cmd.ExecuteScalar().ToString();
}
}
}

Output:

You might also like