0% found this document useful (0 votes)
56 views7 pages

Insertion

The document discusses various ADO.NET concepts like insertion, updation, deletion and selection of data from a SQL database using C# and Windows Forms. It also discusses displaying retrieved data in controls like datagridview, listbox and combobox. It further explains the three tier architecture with separate data, business and presentation layers. Various concepts like stored procedures, parameters, datatables are used for performing CRUD operations on the database.

Uploaded by

Noor Saba
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)
56 views7 pages

Insertion

The document discusses various ADO.NET concepts like insertion, updation, deletion and selection of data from a SQL database using C# and Windows Forms. It also discusses displaying retrieved data in controls like datagridview, listbox and combobox. It further explains the three tier architecture with separate data, business and presentation layers. Various concepts like stored procedures, parameters, datatables are used for performing CRUD operations on the database.

Uploaded by

Noor Saba
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/ 7

ADO.

NET
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace noor
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection(@"Data Source=LAB06-9\SQL2K14;Initial Catalog=noor;Integrated
Security=True");

public Form1()
{
InitializeComponent();
}
Insertion:
private void button1_Click(object sender, EventArgs e)
{
string g = null;
if (rd1.Checked)
g = "Male";
else
g = "Female";
String q = "insert into employee(Name,Gender,Address) values('" + txt1 + "','" + g + "','" + txt2 + "') ";
SqlCommand cmd = new SqlCommand(q, con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
MessageBox.Show("Inserted");
else
MessageBox.Show(" Not Inserted");
}
Updation:
private void button4_Click(object sender, EventArgs e)
{
string g = null;
if (rd1.Checked)
g = "Male";
else
g = "Female";
String q = "update employee set( Name='" + txt1 + "',Gender='" + g + "',Address'" + txt2 + "') where id =1 ";
SqlCommand cmd = new SqlCommand(q, con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
MessageBox.Show("Updated");
else
MessageBox.Show(" Not Updated");
}
}
}
Deletion:
private void button2_Click(object sender, EventArgs e)
{
con.Open();
string query = "delete from Student where SId=1";
SqlCommand cmd = new SqlCommand(query, con);
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
MessageBox.Show("Data Deleted");
else
MessageBox.Show("Data not Deleted ");
}

Selection:
private void Form1_Load(object sender, EventArgs e)
{
string q = "select Id,Name,Address from Student";
SqlCommand cmd = new SqlCommand(q, con);
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(dt);
//dataGridView1.DataSource = dt;

//for combo box binding


comboBox1.DataSource = dt;
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";
}
//for passing data from combo box to list box
private void Click(object sender, EventArgs e)
{
string selectedItem = comboBox1.SelectedItem.ToString();

listBox1.Items.Add(selectedItem);
}

For Showing Data into Data Grid View without Database:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{

public partial class Form1 : Form


{
DataTable dt = new DataTable();
string name = null;
string number;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add("name");
dt.Columns.Add("number");
dt.Columns.Add("gender");
}
private void button1_Click(object sender, EventArgs e)
{
string g = null;
if (radioButton1.Checked == true)
g = "male";
else if (radioButton2.Checked)
g = "female";
else
MessageBox.Show("select gender");
name = textBox1.Text;
number = Convert.ToString(textBox2.Text)
DataRow dr = dt.NewRow();
dr["name"] = name;
dr["number"] = number;
dr["gender"] = g;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
}
}
}

Three Tier:

Data Layer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BO;
using System.Data.SqlClient;

namespace DAL
{
public class DALClass
{
public int InsertData(BOclass b)
{
try {
SqlConnection con = new SqlConnection(@"Data Source=HAIRE;Initial Catalog=SPMor6A;Integrated
Security=True");
SqlCommand cmd = new SqlCommand("InsertData", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@name",b.Name);
cmd.Parameters.AddWithValue("@gender",b.Gender);
cmd.Parameters.AddWithValue("@edu",b.Education);
cmd.Parameters.AddWithValue("@dates",b.Datetime);
con.Open();
int i=cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
return 1;
else
return 0;
}
catch
{
throw;
}
}
}
}

BO layer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BO
{
public class BOclass
{
int id;
string name;
string gender;
string education;
DateTime datetime;

public int Id
{
get
{
return id;
}

set
{
id = value;
}
}

public string Name


{
get
{
return name;
}

set
{
name = value;
}
}

public string Gender


{
get
{
return gender;
}

set
{
gender = value;
}
}

public string Education


{
get
{
return education;
}

set
{
education = value;
}
}

public DateTime Datetime


{
get
{
return datetime;
}

set
{
datetime = value;
}
}
}
}
Buisness Logic Layer:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using BO;
namespace BLL
{
public class BLLClass
{
public int Insert(BOclass b)
{
DALClass d = new DALClass();
int i=d.InsertData(b);
if (i > 0)
return 1;
else
return 0;
}
}
}

Asp.net
protected void form_Load(object sender, EventArgs e)

Int number = TextBox1.Text.ConverToint();

int result = 0;

if (number % 2 == 0)

// Calculate factorial of even number

result = CalculateFactorial(number);

else

// Calculate square root of odd number


result = (int)Math.Sqrt(number);

// Display the result in the textbox

TextBox1.Text = result.ToString();

private int CalculateFactorial(int number)

int result = 1;

while (number > 0)

result *= number;

number--;

return result;

You might also like