using newproform.
studentclass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace newproform
{
public partial class FORMProject : Form
{
public FORMProject()
{
InitializeComponent();
}
Studentclass c = new Studentclass();
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void FORMProject_Load(object sender, EventArgs e)
{
DataTable dt = c.Select();
dataGridView1.DataSource = dt;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataTable dt = c.Select();
dataGridView1.DataSource = dt;
}
private void UPDATE_Click(object sender, EventArgs e)
{
c.RegNo = RegNo.Text;
c.Name = Name.Text;
c.Address = Address.Text;
c.Gender = Gender.Text;
bool success = c.Update(c);
if (success == true)
{
MessageBox.Show("update successfuly");
DataTable dt = c.Select();
dataGridView1.DataSource = dt;
clear();
}
else
{
MessageBox.Show("not updated");
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.Close();
}
public void clear()
{
Name.Text = "";
Address.Text = "";
Gender.Text = "";
RegNo.Text = "";
}
private void DELETE_Click(object sender, EventArgs e)
{
bool success = c.Delete(c);
if (success == true)
{
MessageBox.Show("data deleted successfully");
DataTable dt = c.Select();
dataGridView1.DataSource = dt;
clear();
}
else
{
MessageBox.Show("errro occured");
}
}
private void EMPTY_Click(object sender, EventArgs e)
{
clear();
}
static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;
private void textBox1_TextChanged(object sender, EventArgs e)
{
string keyword = textBox2.Text;
SqlConnection conn = new SqlConnection(myconnstrng);
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM stdtblform WHERE NAME LIKE
'%'+keyword+'%' OR ADDRESS LIKE '%'+keyword+'%'", conn);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
}
private void ADD_Click_1(object sender, EventArgs e)
{
c.Name = Name.Text;
c.Address = Address.Text;
c.Gender = Gender.Text;
bool success = c.Insert(c);
if (success == true)
{
MessageBox.Show("inserted successfully");
clear();
}
else
{
MessageBox.Show("try again");
}
// load data on grid
DataTable dt = c.Select();
dataGridView1.DataSource = dt;
}
}
}
appconfig
<connectionStrings>
<add name="connstrng" connectionString="Data Source=DESKTOP-CVV11TL\
SQLEXPRESS;Initial Catalog=student;Integrated Security=True"/>;
</connectionStrings>
</configuration>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace newproform.studemtclass
{
internal class studentclass
{
public string Name { get; set; }
public int RegNo { get; set; }
public string Address { get; set; }
public string Gender { get; set; }
static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;
private bool isSuccess;
// selecting data from database
public DataTable Select()
{
SqlConnection conn = new SqlConnection(myconnstrng);
DataTable dt = new DataTable();
try
{
string sql = "Select * from stdtblform";
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
conn.Open();
adapter.Fill(dt);
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
return dt;
}
public bool Insert(studentclass c)
{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
string sql = "Insert into stdtblform(name,regno,address,gender)
Values(@name,@regno,@address,@gender)";
SqlCommand cmd = new SqlCommand(sql, conn);
//Parameters adding
cmd.Parameters.AddWithValue("@Name", c.Name);
cmd.Parameters.AddWithValue("RegNo", c.RegNo);
cmd.Parameters.AddWithValue("@Addresss", c.Address);
cmd.Parameters.AddWithValue("@Gender", c.Gender);
conn.Open();
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
return isSuccess;
public bool Update(studentclass c)
{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
string sql = "UPDATE stdtblform SET
Name=@Name ,RegNo=@RegNo ,Address=@Address,Gender=@Gender";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@Name", c.Name);
cmd.Parameters.AddWithValue("@RegNo", c.RegNo);
cmd.Parameters.AddWithValue("@Address", c.Address);
cmd.Parameters.AddWithValue("@Gender", c.Gender);
conn.Open();
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch (Exception eX)
{
}
finally
{
conn.Close();
}
return isSuccess;
}
public bool Delete(studentclass c)
{
bool isSuccess = false;
SqlConnection conn = new SqlConnection(myconnstrng);
try
{
string sql = "Delete from stdtblform Where RegNo=@RegNo";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@RegNo", c.RegNo);
conn.Open();
int rows = cmd.ExecuteNonQuery();
if (rows > 0)
{
isSuccess = true;
}
else
{
isSuccess = false;
}
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}
return isSuccess;
}
}
}