Create a new windows forms Application
Take fout Lablels,four TextBoxes,Four
Buttons on it and set the following Properties
To them
Lable1:
Name : lblEmpId
Text : Enter EmpId Lable2:
Name : lblEName
Text : Enter EName
Lable3:
Name : lblDoj
Text : Enter Doj
Lable1:
Name : lblSalary
Text : Enter Salary
|
textBox1:
Name : txtEmpId
textBox2:
Name : txtUName
textBox3:
Name : txtDoj
textBox1:
Name : txtSalary
|
button1:
Name : btnInsert
Text : Insert
button2:
Name : btnUpdate
Text : Update
button3:
Name : btnDelete
Text : Delete
Button4:
Name : btnClear
Text : Clear
|
Design
:-
Generate the Event Handlers for
btnInsert,btnUpdate,btnDelete,btnClear buttons Click Events
Goto Source & write the following
code
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using System.Data;
using
System.Drawing;
using System.Linq;
using System.Text;
using
System.Windows.Forms;
using
System.Data.SqlClient;
namespace FormNPQInsert
{
public partial class Form1 : Form
{
SqlConnection Con = new
SqlConnection
("Server=HP-PAVILION;User
Id=sa;Password=Admin123;DataBase=Employee"); //for
SqlServer Authentication
//DataSource=.;InitialCatalog=EmpDetails;IntegratedSecurity=True;/*For Windows
Authentication*/
SqlCommand Cmd;
public
Form1()
{
InitializeComponent();
}
private void btnInsert_Click(object
sender, EventArgs e)
{
string
q = "insert into EmpDetails values("+txtEmpId.Text+",'"+txtEName.Text+"','"+txtDoj.Text+"',"+txtSalary.Text+")";
Cmd = new
SqlCommand(q,Con);
Con.Open();
int
r = Cmd.ExecuteNonQuery();
MessageBox.Show(r+"
Record(s) Inderted Successfully");
Con.Close();
}
private
void btnUpdate_Click(object
sender, EventArgs e)
{
string q = "Update
EmpDetails set EName='" + txtEName.Text +
"',Doj='"
+ txtDoj.Text + "',Salary=" +
txtSalary.Text + " where EmpId="
+ txtEmpId.Text + "";
Cmd = new
SqlCommand(q,Con);
Con.Open();
int
r = Cmd.ExecuteNonQuery();
Con.Close();
MessageBox.Show(r+" Record(s)
Updated Successfully");
}
private
void btnDelete_Click(object
sender, EventArgs e)
{
string
q = "Delete from EmpDetails where
EmpId="+txtEmpId.Text+"";
Cmd = new
SqlCommand(q,Con);
Con.Open();
int
r = Cmd.ExecuteNonQuery();
Con.Close();
MessageBox.Show(r+" Record(s)
Deleted Successfully ");
}
private
void btnClear_Click(object
sender, EventArgs e)
{
foreach
(Control x in
this.Controls)
{
if
(x is TextBox)
x.Text = "";
}
}
}
|
No comments:
Post a Comment