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.Sql;
using System.Data.SqlClient;
using System.Configuration;
namespace Students_Desk
{
public partial class frmStudents : Form
{
string CS = ConfigurationManager.ConnectionStrings["StudentDB"].ConnectionString;
SqlConnection conn;
SqlCommand cmd;
SqlDataAdapter da;
SqlDataReader rdr;
public frmStudents()
{
InitializeComponent();
}
private void frmStudents_Load(object sender, EventArgs e)
{
conn = new SqlConnection(CS);
conn.Open();
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from tblInformation ", conn);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
private void btnInsert_Click(object sender, EventArgs e)
{
if (txtName.Text == string.Empty || txtAge.Text == string.Empty || txtGender.Text == string.Empty || txtCity.Text == string.Empty)
{
MessageBox.Show("Data fields cannot be blank!", "info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
try
{
conn = new SqlConnection(CS);
conn.Open();
cmd = new SqlCommand("Insert into tblInformation values(@Name, @Age, @Gender, @City)", conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Age", txtAge.Text);
cmd.Parameters.AddWithValue("@Gender", txtGender.Text);
cmd.Parameters.AddWithValue("@City", txtCity.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Data saved Successfully!", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from tblInformation ", conn);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
ClearFields();
}
private void ClearFields()
{
txtAge.Clear();
txtName.Clear();
txtCity.Clear();
txtGender.Clear();
txtSearch.Clear();
}
private void btnExit_Click(object sender, EventArgs e)
{
DialogResult d = MessageBox.Show("Do you want to close Application?", "Inform", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (d == DialogResult.Yes)
{
Application.Exit();
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
if (txtSearch.Text == string.Empty)
{
MessageBox.Show("Please enter the ID to be updated!", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
try
{
conn = new SqlConnection(CS);
conn.Open();
cmd = new SqlCommand("Update tblInformation set Name=@Name, Age=@Age, Gender=@Gender, City=@City where ID='" + txtSearch.Text + "'", conn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Age", txtAge.Text);
cmd.Parameters.AddWithValue("@Gender", txtGender.Text);
cmd.Parameters.AddWithValue("@City", txtCity.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Data Updated successfully!", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
ClearFields();
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from tblInformation ", conn);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void btnSearch_Click(object sender, EventArgs e)
{
conn = new SqlConnection(CS);
conn.Open();
if (txtSearch.Text == string.Empty)
{
MessageBox.Show("Please enter the ID to search!", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
cmd = new SqlCommand("select * from tblInformation where ID ='" + txtSearch.Text + "' ", conn);
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
txtName.Text = rdr[1].ToString();
txtAge.Text = rdr[2].ToString();
txtGender.Text = rdr[3].ToString();
txtCity.Text = rdr[4].ToString();
}
}
else
{
MessageBox.Show("User ID doesn't exist!", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
conn = new SqlConnection(CS);
conn.Open();
if (txtSearch.Text == string.Empty)
{
MessageBox.Show("Please enter the ID to be deleted!", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
try
{
DialogResult d = MessageBox.Show("Do you want to Delete User having ID " + txtSearch.Text, "Inform", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (d == DialogResult.Yes)
{
cmd = new SqlCommand("delete from tblInformation where ID='" + txtSearch.Text + "'", conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
MessageBox.Show("Record deleted successfully!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
ClearFields();
DataTable dt = new DataTable();
da = new SqlDataAdapter("select * from tblInformation ", conn);
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}