Watch the designing part in video!
Connection Class Code:
Source Code of the program!
Connection Class Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace projectincsharp
{
class Connection
{
SqlConnection con = new SqlConnection("Data Source=RAN-PC;Initial Catalog=projectincsharp;User ID=sa;Password=interrupt");
public SqlConnection active()
{
if(con.State==ConnectionState.Closed)
{
con.Open();
}
return con;
}
}
}
Source Code of the program!
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;
namespace projectincsharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
fillListBox();
}
private void btnInsert_Click(object sender, EventArgs e)
{
if (validate())
{
Connection con = new Connection();
SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].[project]
([Name]
,[Address]
,[Email])
VALUES
('" + txtName.Text + "', '" + txtAdd.Text + "', '" + txtEmail.Text + "')", con.active());
cmd.ExecuteNonQuery();
//cmd.EndExecuteNonQuery();
MessageBox.Show("successfully", "insert", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("error", "insert", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
bool validate()
{
bool returnval = true;
if(txtName.Text.Length==0)
{
returnval = false;
}
if(txtAdd.Text.Length==0)
{
returnval = false;
}
if(txtEmail.Text.Length==0)
{
returnval = false;
}
return returnval;
}
private void btnDelete_Click(object sender, EventArgs e)
{
Connection con = new Connection();
SqlCommand cmd = new SqlCommand(@"DELETE FROM [dbo].[project] WHERE [Roll No] = '" + txtRoll.Text + "'",con.active());
cmd.ExecuteNonQuery();
MessageBox.Show("successfull", "delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtRoll.Clear();
txtName.Clear();
txtAdd.Clear();
txtEmail.Clear();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
Connection con = new Connection();
SqlCommand cmd = new SqlCommand(@"UPDATE [dbo].[project]
SET [Name] = '" + txtName.Text + "',[Address] = '" + txtAdd.Text + "',[Email] = '" +txtEmail.Text+"' WHERE [Roll No] ='" + txtRoll.Text + "'", con.active());
cmd.ExecuteNonQuery();
MessageBox.Show("successful", "update", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnNew_Click(object sender, EventArgs e)
{
txtAdd.Clear();
txtEmail.Clear();
txtRoll.Clear();
txtName.Clear();
txtName.Focus();
}
void GridView()
{
Connection con = new Connection();
SqlDataAdapter sda = new SqlDataAdapter("select * from project", con.active());
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.Rows.Clear();
foreach(DataRow item in dt.Rows)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = item["Roll No"].ToString();
dataGridView1.Rows[n].Cells[1].Value = item["Name"].ToString();
dataGridView1.Rows[n].Cells[2].Value = item["Address"].ToString();
dataGridView1.Rows[n].Cells[3].Value = item["Email"].ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
GridView();
}
private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
int n = dataGridView1.SelectedRows[0].Index;
txtRoll.Text = dataGridView1.Rows[n].Cells[0].Value.ToString();
txtName.Text = dataGridView1.Rows[n].Cells[1].Value.ToString();
txtAdd.Text = dataGridView1.Rows[n].Cells[2].Value.ToString();
txtEmail.Text = dataGridView1.Rows[n].Cells[3].Value.ToString();
}
void fillListBox()
{
string query = "select * from project";
Connection con = new Connection();
SqlCommand cmd = new SqlCommand(query, con.active());
SqlDataReader myreader;
try
{
con.active();
myreader = cmd.ExecuteReader();
while(myreader.Read())
{
string sname = myreader.GetString(1);
listBox1.Items.Add(sname);
}
} catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Connection con = new Connection();
string sql = "select * from project where name ='" + listBox1.Text + "';";
SqlCommand cmd = new SqlCommand(sql, con.active());
SqlDataReader myreader;
try
{
con.active();
myreader = cmd.ExecuteReader();
while(myreader.Read())
{
string rollno = myreader.GetInt32(0).ToString();
string name = myreader.GetString(1);
string address= myreader.GetString(2);
string email = myreader.GetString(3);
txtRoll.Text = rollno;
txtName.Text = name;
txtAdd.Text = address;
txtEmail.Text = email;
}
}catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
No comments:
Post a Comment