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 PublicLibraryManagementSystem
{
public partial class FormLibrarian : Form
{
private FormAdminHome Home { get; set; }
private DataAccess Da { get; set; }
public FormLibrarian()
{
InitializeComponent();
this.Da = new DataAccess();
this.PopulateGridView();
}
public FormLibrarian(FormAdminHome home)
{
InitializeComponent();
this.Home = home;
}
private void BtnHome_Click(object sender, EventArgs e)
{
this.Home.Show();
this.Hide();
}
private void PopulateGridView(string sql = "select * from Personnel where Role='Librarian';")
{
var ds = this.Da.ExecuteQuery(sql);
this.dgvLibrarian.AutoGenerateColumns = false;
this.dgvLibrarian.DataSource = ds.Tables[0];
}
private void dgvLibrarian_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void btnShow_Click(object sender, EventArgs e)
{
this.PopulateGridView();
}
private void txtSearchLibrarian_TextChanged(object sender, EventArgs e)
{
var sql = "select * from Personnel where UserID like '" + this.txtSearchLibrarian.Text + "%';";
this.PopulateGridView(sql);
}
private void BtnAddLibrarian_Click(object sender, EventArgs e)
{
try
{
if (!this.IsValidToSave())
{
MessageBox.Show("Empty fields are not allowed. Please, fill all the information.");
return;
}
var sql = "insert into Personnel values('" + this.txtLibrarianID.Text + "', '" + this.txtLibrarianName.Text + "', '" + this.txtLibrarianPassword.Text + "', 'Librarian', '" + this.txtLibrarianPhone.Text + "', '" + this.txtLibrarianAddress.Text + "', '" + this.txtLibrarianEmail.Text + "'); ";
var count = this.Da.ExecuteDMLQuery(sql);
if (count == 1)
MessageBox.Show("Librarian Added!");
else
MessageBox.Show("Failed to add! Try again.");
this.PopulateGridView();
this.ClearAll();
}
catch (Exception exc)
{
MessageBox.Show("An error has occured due to invalid input.\nError Info:" + exc.Message);
}
}
private bool IsValidToSave()
{
if (String.IsNullOrEmpty(this.txtLibrarianID.Text) || String.IsNullOrEmpty(this.txtLibrarianName.Text) ||String.IsNullOrEmpty(this.txtLibrarianPassword.Text) || String.IsNullOrEmpty(this.txtLibrarianEmail.Text) || String.IsNullOrEmpty(this.txtLibrarianPhone.Text) || String.IsNullOrEmpty(this.txtLibrarianAddress.Text))
{
return false;
}
else
return true;
}
private void ClearAll()
{
this.txtLibrarianID.Clear();
this.txtLibrarianName.Clear();
this.txtLibrarianPassword.Clear();
this.txtLibrarianEmail.Clear();
this.txtLibrarianPhone.Clear();
this.txtLibrarianAddress.Clear();;
this.txtSearchLibrarian.Clear();
this.dgvLibrarian.ClearSelection();
}
private void BtnDeleteLibrarian_Click(object sender, EventArgs e)
{
try
{
if (this.dgvLibrarian.SelectedRows.Count == 0)
{
MessageBox.Show("Please, select a row first to delete!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var id = this.dgvLibrarian.CurrentRow.Cells[0].Value.ToString();
var name = this.dgvLibrarian.CurrentRow.Cells["Username"].Value.ToString();
DialogResult result = MessageBox.Show($"Are you sure you want to delete {name}?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.No)
{
return;
}
var sql = "delete from Personnel where UserID = '" + id + "';";
var count = this.Da.ExecuteDMLQuery(sql);
if (count == 1)
MessageBox.Show(name + " as Librarian deleted properly.");
else
MessageBox.Show("Failed to delete! Try again.");
this.PopulateGridView();
this.ClearAll();
}
catch (Exception exc)
{
MessageBox.Show("An error has occured due to invalid choice.\nError Info:" + exc.Message);
}
}
private void FormLibrarian_Load(object sender, EventArgs e)
{
this.PopulateGridView();
this.dgvLibrarian.ClearSelection();
this.AutoIdGenerate();
}
private void dgvLibrarian_DoubleClick(object sender, EventArgs e)
{
this.txtLibrarianID.Text = this.dgvLibrarian.CurrentRow.Cells[0].Value.ToString();
this.txtLibrarianName.Text = this.dgvLibrarian.CurrentRow.Cells[1].Value.ToString();
this.txtLibrarianPassword.Text = this.dgvLibrarian.CurrentRow.Cells[2].Value.ToString();
this.txtLibrarianEmail.Text = this.dgvLibrarian.CurrentRow.Cells[4].Value.ToString();
this.txtLibrarianPhone.Text = this.dgvLibrarian.CurrentRow.Cells[5].Value.ToString();
this.txtLibrarianAddress.Text = this.dgvLibrarian.CurrentRow.Cells[6].Value.ToString();
}
private void AutoIdGenerate()
{
}
}
}
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 PublicLibraryManagementSystem
{
public partial class FormLibrarian : Form
{
private FormAdminHome Home { get; set; }
private DataAccess Da { get; set; }
public FormLibrarian()
{
InitializeComponent();
this.Da = new DataAccess();
this.PopulateGridView();
}
public FormLibrarian(FormAdminHome home)
{
InitializeComponent();
this.Home = home;
}
private void BtnHome_Click(object sender, EventArgs e)
{
this.Home.Show();
this.Hide();
}
private void PopulateGridView(string sql = "select * from Personnel where Role='Librarian';")
{
var ds = this.Da.ExecuteQuery(sql);
this.dgvLibrarian.AutoGenerateColumns = false;
this.dgvLibrarian.DataSource = ds.Tables[0];
}
private void dgvLibrarian_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void btnShow_Click(object sender, EventArgs e)
{
this.PopulateGridView();
}
private void txtSearchLibrarian_TextChanged(object sender, EventArgs e)
{
var sql = "select * from Personnel where UserID like '" + this.txtSearchLibrarian.Text + "%';";
this.PopulateGridView(sql);
}
private void BtnAddLibrarian_Click(object sender, EventArgs e)
{
try
{
if (!this.IsValidToSave())
{
MessageBox.Show("Empty fields are not allowed. Please, fill all the information.");
return;
}
var sql = "insert into Personnel values('" + this.txtLibrarianID.Text + "', '" + this.txtLibrarianName.Text + "', '" + this.txtLibrarianPassword.Text + "', 'Librarian', '" + this.txtLibrarianPhone.Text + "', '" + this.txtLibrarianAddress.Text + "', '" + this.txtLibrarianEmail.Text + "'); ";
var count = this.Da.ExecuteDMLQuery(sql);
if (count == 1)
MessageBox.Show("Librarian Added!");
else
MessageBox.Show("Failed to add! Try again.");
this.PopulateGridView();
this.ClearAll();
}
catch (Exception exc)
{
MessageBox.Show("An error has occured due to invalid input.\nError Info:" + exc.Message);
}
}
private bool IsValidToSave()
{
if (String.IsNullOrEmpty(this.txtLibrarianID.Text) || String.IsNullOrEmpty(this.txtLibrarianName.Text) ||String.IsNullOrEmpty(this.txtLibrarianPassword.Text) || String.IsNullOrEmpty(this.txtLibrarianEmail.Text) || String.IsNullOrEmpty(this.txtLibrarianPhone.Text) || String.IsNullOrEmpty(this.txtLibrarianAddress.Text))
{
return false;
}
else
return true;
}
private void ClearAll()
{
this.txtLibrarianID.Clear();
this.txtLibrarianName.Clear();
this.txtLibrarianPassword.Clear();
this.txtLibrarianEmail.Clear();
this.txtLibrarianPhone.Clear();
this.txtLibrarianAddress.Clear();;
this.txtSearchLibrarian.Clear();
this.dgvLibrarian.ClearSelection();
}
private void BtnDeleteLibrarian_Click(object sender, EventArgs e)
{
try
{
if (this.dgvLibrarian.SelectedRows.Count == 0)
{
MessageBox.Show("Please, select a row first to delete!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var id = this.dgvLibrarian.CurrentRow.Cells[0].Value.ToString();
var name = this.dgvLibrarian.CurrentRow.Cells["Username"].Value.ToString();
DialogResult result = MessageBox.Show($"Are you sure you want to delete {name}?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.No)
{
return;
}
var sql = "delete from Personnel where UserID = '" + id + "';";
var count = this.Da.ExecuteDMLQuery(sql);
if (count == 1)
MessageBox.Show(name + " as Librarian deleted properly.");
else
MessageBox.Show("Failed to delete! Try again.");
this.PopulateGridView();
this.ClearAll();
}
catch (Exception exc)
{
MessageBox.Show("An error has occured due to invalid choice.\nError Info:" + exc.Message);
}
}
private void FormLibrarian_Load(object sender, EventArgs e)
{
this.PopulateGridView();
this.dgvLibrarian.ClearSelection();
this.AutoIdGenerate();
}
private void dgvLibrarian_DoubleClick(object sender, EventArgs e)
{
this.txtLibrarianID.Text = this.dgvLibrarian.CurrentRow.Cells[0].Value.ToString();
this.txtLibrarianName.Text = this.dgvLibrarian.CurrentRow.Cells[1].Value.ToString();
this.txtLibrarianPassword.Text = this.dgvLibrarian.CurrentRow.Cells[2].Value.ToString();
this.txtLibrarianEmail.Text = this.dgvLibrarian.CurrentRow.Cells[4].Value.ToString();
this.txtLibrarianPhone.Text = this.dgvLibrarian.CurrentRow.Cells[5].Value.ToString();
this.txtLibrarianAddress.Text = this.dgvLibrarian.CurrentRow.Cells[6].Value.ToString();
}
private void AutoIdGenerate()
{
}
}
}
Error:
- Exception Text ************** System.NullReferenceException: Object reference not set to an instance of an object. at PublicLibraryManagementSystem.FormLibrarian.PopulateGridView(String sql) in C:\Users\GIGABYTE\Desktop\New
I am trying to make the data grid view work.but when I am trying to show button it is saying null reference