You are on page 1of 10

18MCA5PCWP: WINDOWS APPLICATION PROGRAMMING USING C#.

NET 1BM19MCA16

Part-B
1. Build an application using Windows Programming and Database connectivity with
ADO.NET.

Code:
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace CRUD_ADO_
{
public partial class Form1 : Form
{
public static SqlConnection? conn;
public Form1()
{
InitializeComponent();
}

private void button4_Click(object sender, EventArgs e)


{
try
{
string query = "select * from emp where empid='" + textBox1.Text + "'";

SqlCommand cmd = new SqlCommand(query, conn);

SqlDataAdapter sda = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

sda.Fill(dt);

dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex+"");
}

private void button1_Click(object sender, EventArgs e)


{
try
{
string query = "insert into emp values('" + textBox1.Text + "','" + textBox2.Text + "','"+textBox3.Text+"')";

SqlCommand cmd = new SqlCommand(query, conn);

cmd.ExecuteNonQuery();

MessageBox.Show("Employee Record Inserted ");

textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}

catch (Exception x)
{

BMS COLLEGE OF ENGINEERING BANGALORE 1


18MCA5PCWP: WINDOWS APPLICATION PROGRAMMING USING C#.NET 1BM19MCA16

MessageBox.Show(x+"Please Fill The Form With Valid Details");

}
}

private void Form1_Load(object sender, EventArgs e)


{
radioButton1.Checked = true;
button3.Enabled = true;
button4.Enabled = false;
button1.Enabled = true;
button2.Enabled = false;
try
{
string Source = "server=.;Initial Catalog=employee;Integrated Security=True";
conn = new SqlConnection(Source);
conn.Open();
}
catch (Exception x){
MessageBox.Show(x+"");
}

private void button2_Click(object sender, EventArgs e)


{
try
{
string query = "delete from emp where empid='"+textBox1.Text+"'";

SqlCommand cmd = new SqlCommand(query, conn);

cmd.ExecuteNonQuery();

MessageBox.Show("Employee With id "+textBox1.Text+" deleted");

textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}

catch (Exception x)
{

MessageBox.Show(x + "Please Fill The Form With Valid Details");

}
}

private void button3_Click(object sender, EventArgs e)


{
try
{
string query = "update emp set empname='"+textBox2.Text+"',age='"+textBox3.Text+"' where
empid='"+textBox1.Text+"'";

SqlCommand cmd = new SqlCommand(query, conn);

cmd.ExecuteNonQuery();

MessageBox.Show("Record Updated With id "+textBox1.Text);

BMS COLLEGE OF ENGINEERING BANGALORE 2


18MCA5PCWP: WINDOWS APPLICATION PROGRAMMING USING C#.NET 1BM19MCA16

textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}

catch (Exception x)
{

MessageBox.Show(x + "Please Fill The Form With Valid Details");

}
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)


{
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
button3.Enabled = true;
button4.Enabled = false;
button1.Enabled = true;
button2.Enabled = false;

private void radioButton3_CheckedChanged(object sender, EventArgs e)


{
if (radioButton3.Checked)
MessageBox.Show("Enter id For Search and Delete");
textBox2.Enabled = false;
textBox3.Enabled = false;
button3.Enabled = false;
button4.Enabled = true;
button1.Enabled = false;
button2.Enabled = true;
}
}
}

Output-

UI-

BMS COLLEGE OF ENGINEERING BANGALORE 3


18MCA5PCWP: WINDOWS APPLICATION PROGRAMMING USING C#.NET 1BM19MCA16

BMS COLLEGE OF ENGINEERING BANGALORE 4


18MCA5PCWP: WINDOWS APPLICATION PROGRAMMING USING C#.NET 1BM19MCA16

Database-

BMS COLLEGE OF ENGINEERING BANGALORE 5


18MCA5PCWP: WINDOWS APPLICATION PROGRAMMING USING C#.NET 1BM19MCA16

2. Build an application using ASP.NET techniques with appropriate validations and Database
connectivity with ADO.NET

Code:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Part2
{
public partial class WebForm1 : System.Web.UI.Page
{
public static SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
string Source ="server =.; Initial Catalog = Student; Integrated Security = True ";
conn = new SqlConnection(Source);
conn.Open();
}

protected void Button1_Click(object sender, EventArgs e)


{
string query = "insert into stu values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','"
+ TextBox4.Text + "','" + TextBox5.Text + "')";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
Messagebox("Data inserted Successfully");
TextBox1.Text = " ";
TextBox2.Text = " ";
TextBox3.Text = " ";
TextBox4.Text = " ";
TextBox5.Text = " ";
TextBox6.Text = " ";

}
public void Messagebox(string xMessage)
{
Response.Write("<script>alert('" + xMessage + "')</script>");
}

}
}

Design code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="Part2.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>

BMS COLLEGE OF ENGINEERING BANGALORE 6


18MCA5PCWP: WINDOWS APPLICATION PROGRAMMING USING C#.NET 1BM19MCA16

<body>
<form id="form1" runat="server">
<div style="height: 399px">
<asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" style="margin-left: 84px" Width="339px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="First Name Cant be Blank" Display="Dynamic" ForeColor="Red"
SetFocusOnError="True"></asp:RequiredFieldValidator>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" style="margin-left: 86px" Width="339px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2"
ErrorMessage="Last Name Cant be Blank" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<br />

Age&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox3" runat="server" Width="331px"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox3"
ErrorMessage="Age should be between 18 to 60" ForeColor="Red" MaximumValue="60"
MinimumValue="18"></asp:RangeValidator>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Email"></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox4" runat="server" Width="342px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox4"
ErrorMessage="Email cant be Empty" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<br />
<asp:Label ID="Label4" runat="server" Text="Password"></asp:Label>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox5" runat="server" Width="338px" TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:Label ID="Label5" runat="server" Text="Confirm Password"></asp:Label>
&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox6" runat="server" style="margin-left: 21px" Width="330px"
TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox5"
ControlToValidate="TextBox6" ErrorMessage="Password doesnt Match"
ForeColor="Red"></asp:CompareValidator>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Height="50px" Text="Submit" Width="181px"
OnClick="Button1_Click" />
</div>
</form>

</body>
</html>

BMS COLLEGE OF ENGINEERING BANGALORE 7


18MCA5PCWP: WINDOWS APPLICATION PROGRAMMING USING C#.NET 1BM19MCA16

OUTPUT:

BMS COLLEGE OF ENGINEERING BANGALORE 8


18MCA5PCWP: WINDOWS APPLICATION PROGRAMMING USING C#.NET 1BM19MCA16

DataBase:

BMS COLLEGE OF ENGINEERING BANGALORE 9


18MCA5PCWP: WINDOWS APPLICATION PROGRAMMING USING C#.NET 1BM19MCA16

1
BMS COLLEGE OF ENGINEERING BANGALORE
0

You might also like