I am developing a solution that has master and addEmployee as a table and a form respectively. The solution will save data into master using addEmployee form. The solution uses C# as front-end and sql2008 database as back-end. After compiling the solution, the back-end does not have any data and i receive an error "
Additional information: The connection was not closed. The connection's current state is open.
. Please can anyone help me?
What I have tried:
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 FHAMortgageBank
{
public partial class Add_Employee : Form
{
SqlConnection con = new SqlConnection("Data Source=AKINTOMIDE-HP\\SQLEXPRESS;Initial Catalog=fham;Integrated Security=True");
SqlCommand cmd;
SqlDataAdapter adapt;
private DataViewManager dviewmanager;
int SN = 0;
public Add_Employee()
{
InitializeComponent();
ClearData();
FillBank();
FillStatus();
FillPenAdmin();
FillYear();
FillGrade();
FillDpt();
}
private void frmMain_Enter(object sender, EventArgs e)
{
}
private void BntSave_Click(object sender, EventArgs e)
{
if (this.txteid.Text != "" && txtStaf.Text != "")
{
con.Open();
cmd = new SqlCommand("insert into master (eid,mon,yr,dat,staf,adrs,coy,loc,dpt,grd,datead,datemod,bsal,tran,hous,lunc,uti,furn,harz,educ,housM,dres,risk,veh,driv,dome,pfs,nhf,nhfs,nhis,oded,otherpay,housU,dressU,staffM,Thrift,accno,bank,email,penadmin,pennum,status,dupfront,picstaffpix) values(@eid,@mon,@yr,@dat,@staf,@adrs,@coy,@loc,@dpt,@grd,@datead,@datemod,@bsal,@tran,@hous,@lunc,@uti,@furn,@harz,@educ,@housM,@dres,@risk,@veh,@driv,@dome,@pfs,@nhf,@nhfs,@nhis,@oded,@otherpay,@housU,@dressU,@staffM,@Thrift,@accno,@bank,@email,@penadmin,@pennum,@status,@dupfront,@picstaffpix)", con);
cmd.Parameters.AddWithValue("@eid", txteid.Text);
cmd.Parameters.AddWithValue("@mon", cmbMon.Text);
cmd.Parameters.AddWithValue("@yr", cmbYr.Text);
cmd.Parameters.AddWithValue("@dat", SqlDbType.Date);
cmd.Parameters.AddWithValue("@datead", SqlDbType.DateTime);
cmd.Parameters.AddWithValue("@datemod", SqlDbType.DateTime);
cmd.Parameters.AddWithValue("@staf", txtStaf.Text);
cmd.Parameters.AddWithValue("@adrs", txtAdrs);
cmd.Parameters.AddWithValue("@coy", txtCoy.Text);
cmd.Parameters.AddWithValue("@dpt", cmbDpt.Text);
cmd.Parameters.AddWithValue("@loc", cmbLoc.Text);
cmd.Parameters.AddWithValue("@grd", CmbGrd.Text);
cmd.Parameters.AddWithValue("@accno", TxtAccno.Text);
cmd.Parameters.AddWithValue("@email", txtemail.Text);
cmd.Parameters.AddWithValue("@picstaffpix", picStafPix);
cmd.Parameters.AddWithValue("@bank", cmbBank.Text);
cmd.Parameters.AddWithValue("@penadmin", cmbPenAdmin.Text);
cmd.Parameters.AddWithValue("@status", cmbStatus.Text);
cmd.Parameters.AddWithValue("@pennum", txtPenNum.Text);
cmd.Parameters.AddWithValue("@basl", txtBSal.Text);
cmd.Parameters.AddWithValue("@dres", txtDres.Text);
cmd.Parameters.AddWithValue("@dupfront", txtDUpfront.Text);
cmd.Parameters.AddWithValue("@educ", txtEduc.Text);
cmd.Parameters.AddWithValue("@furn", txtFurn.Text);
cmd.Parameters.AddWithValue("@harz", txtHarz.Text);
cmd.Parameters.AddWithValue("@tran", txtTran.Text);
cmd.Parameters.AddWithValue("@hous", txtHous.Text);
cmd.Parameters.AddWithValue("@lunc", txtLunc.Text);
cmd.Parameters.AddWithValue("@uti", txtUti.Text);
cmd.Parameters.AddWithValue("@housM", txtHousM.Text);
cmd.Parameters.AddWithValue("@risk", txtRisk.Text);
cmd.Parameters.AddWithValue("@veh", txtVeh.Text);
cmd.Parameters.AddWithValue("@pfs", txtPfS.Text);
cmd.Parameters.AddWithValue("@nhf", txtNHF.Text);
cmd.Parameters.AddWithValue("@nhfs", txtNHFS.Text);
cmd.Parameters.AddWithValue("@oded", txtOded.Text);
cmd.Parameters.AddWithValue("@pens", txtPens.Text);
cmd.Parameters.AddWithValue("@PAYE", txtPAYE.Text);
cmd.Parameters.AddWithValue("@tded", txtTDed.Text);
cmd.Parameters.AddWithValue("@driv", txtDriv.Text);
cmd.Parameters.AddWithValue("@dome", txtDome.Text);
cmd.Parameters.AddWithValue("@tpay", txtTPay.Text);
cmd.Parameters.AddWithValue("@npay", txtNPay.Text);
cmd.Parameters.AddWithValue("@housU", this.txtHousU.Text);
cmd.Parameters.AddWithValue("@housM", this.txtHousM.Text);
cmd.Parameters.AddWithValue("@staffM", this.txtStaffM.Text);
cmd.Parameters.AddWithValue("@thrift", this.txtThrift.Text);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Inserted Successfully");
ClearData();
}
else
{
MessageBox.Show("Please Provide Details!");
}
if (this.txteid.Text != "" && txtStaf.Text != "")
{
MessageBox.Show("Please provide Status");
return;
}
}
private void DisplayData()
{
}
private void ClearData()
{
txteid.Text = "FHA136";
txtStaf.Text = "Akintomide Ayodele";
txtAdrs.Text = "39 Durban Street, Wuse II, Abuja";
txtCoy.Text = "FHA Homes Ltd";
TxtAccno.Text = "11234567";
txtemail.Text = "info@fhamortgage.gov.ng";
txtBSal.Text = "0";
txtDres.Text = "0";
txtDUpfront.Text = "0";
txtEduc.Text = "0";
txtFurn.Text = "0";
txtHarz.Text = "0";
txtHous.Text = "0";
txtHousM.Text = "0";
txtHousU.Text = "0";
txtLunc.Text = "0";
txtNHF.Text = "0";
txtNHFS.Text = "0";
txtOded.Text = "0";
txtOtherPay.Text = "0";
txtPenNum.Text="pen1234";
txtPAYE.Text = "0";
txtPens.Text = "0";
txtPfS.Text = "0";
txtRisk.Text = "0";
txtStaffM.Text = "0";
txtThrift.Text = "0";
txtTran.Text = "0";
txtUti.Text = "0";
txtVeh.Text = "0";
datAd.Text = DateTime.Now.Date.ToShortDateString();
DatMod.Text = DateTime.Now.Date.ToShortDateString();
datEmp.Text = DateTime.Now.Date.ToShortDateString();
cmbBank.Text = "United Bank of Africa";
cmbDpt.Text = "Operations";
CmbGrd.Text = "Banking Officer";
cmbLoc.Text = "Abuja";
cmbMon.Text = DateTime.Now.Date.ToShortDateString();
cmbPenAdmin.Text = "Crusader";
cmbStatus.Text = "staff";
cmbYr.Text = DateTime.Now.Date.ToShortDateString();
txtTDed.Text = "0";
txtDriv.Text = "0";
txtDome.Text= "0";
txtTPay.Text = "0";
txtNPay.Text= "0";
SN = 0;
}
private void New_Rec()
{
}
private void Add_Employee_Load(object sender, EventArgs e)
{
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sqlLoc = "SELECT * FROM Location";
var lAdapter = new SqlDataAdapter(sqlLoc, con);
var lset = new DataSet();
lAdapter.TableMappings.Add("Table", "Location");
lAdapter.Fill(lset);
this.dviewmanager = lset.DefaultViewManager;
try
{
for (int i = 0; i <= lset.Tables[0].Rows.Count - 1; i++)
{
this.cmbLoc.Items.Add(lset.Tables[0].Rows[i]["Location"].ToString());
}
}
catch (Exception exl)
{
}
this.cmbMon.Items.Add("January");
this.cmbMon.Items.Add("February");
this.cmbMon.Items.Add("March");
this.cmbMon.Items.Add("April");
this.cmbMon.Items.Add("May");
this.cmbMon.Items.Add("June");
this.cmbMon.Items.Add("July");
this.cmbMon.Items.Add("August");
this.cmbMon.Items.Add("September");
this.cmbMon.Items.Add("October");
this.cmbMon.Items.Add("November");
this.cmbMon.Items.Add("December");
}
}
protected void FillBank()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sqlStr = "SELECT * FROM BANK";
var dAdapter = new SqlDataAdapter(sqlStr, con);
var dset = new DataSet();
dAdapter.TableMappings.Add("Table", "Bank");
dAdapter.Fill(dset);
this.dviewmanager = dset.DefaultViewManager;
try
{
for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++)
{
this.cmbBank.Items.Add(dset.Tables[0].Rows[i]["Bank"].ToString());
}
}
catch (Exception ex)
{
}
}
protected void FillDpt()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sqlStr = "SELECT * FROM Dpt";
var dAdapter = new SqlDataAdapter(sqlStr, con);
var dset = new DataSet();
dAdapter.TableMappings.Add("Table", "Dpt");
dAdapter.Fill(dset);
this.dviewmanager = dset.DefaultViewManager;
try
{
for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++)
{
this.cmbDpt.Items.Add(dset.Tables[0].Rows[i]["Dpt"].ToString());
}
}
catch (Exception ex)
{
}
}
protected void FillYear()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sqlStr = "SELECT * FROM Year";
var dAdapter = new SqlDataAdapter(sqlStr, con);
var dset = new DataSet();
dAdapter.TableMappings.Add("Table", "Year");
dAdapter.Fill(dset);
this.dviewmanager = dset.DefaultViewManager;
try
{
for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++)
{
this.cmbYr.Items.Add(dset.Tables[0].Rows[i]["Year"].ToString());
}
}
catch (Exception ex)
{
}
}
protected void FillGrade()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sqlStr = "SELECT * FROM Grade";
var dAdapter = new SqlDataAdapter(sqlStr, con);
var dset = new DataSet();
dAdapter.TableMappings.Add("Table", "Grade");
dAdapter.Fill(dset);
this.dviewmanager = dset.DefaultViewManager;
try
{
for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++)
{
this.CmbGrd.Items.Add(dset.Tables[0].Rows[i]["Grade"].ToString());
}
}
catch (Exception ex)
{
}
}
protected void FillStatus()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sqlStr = "SELECT * FROM Status";
var dAdapter = new SqlDataAdapter(sqlStr, con);
var dset = new DataSet();
dAdapter.TableMappings.Add("Table", "Status");
dAdapter.Fill(dset);
this.dviewmanager = dset.DefaultViewManager;
try
{
for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++)
{
this.cmbStatus.Items.Add(dset.Tables[0].Rows[i]["Status"].ToString());
}
}
catch (Exception ex)
{
}
}
protected void FillPenAdmin()
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sqlStr = "SELECT * FROM PenAdmin";
var dAdapter = new SqlDataAdapter(sqlStr, con);
var dset = new DataSet();
dAdapter.TableMappings.Add("Table", "PenAdmin");
dAdapter.Fill(dset);
this.dviewmanager = dset.DefaultViewManager;
try
{
for (int i = 0; i <= dset.Tables[0].Rows.Count - 1; i++)
{
this.cmbPenAdmin.Items.Add(dset.Tables[0].Rows[i]["PenAdmin"].ToString());
}
}
catch (Exception ex)
{
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void label14_Click(object sender, EventArgs e)
{
}
private void frmTpay_Enter(object sender, EventArgs e)
{
}
private void txtHousU_TextChanged(object sender, EventArgs e)
{
}
private void label33_Click(object sender, EventArgs e)
{
}
private void txtStaffM_TextChanged(object sender, EventArgs e)
{
}
private void txtPAYE_TextChanged(object sender, EventArgs e)
{
}
private void datEmp_ValueChanged(object sender, EventArgs e)
{
}
private void btnUpdate_Click(object sender, EventArgs e)
{
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void btnSearch_Click(object sender, EventArgs e)
{
}
private void picStafPix_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
this.picStafPix.Image = new Bitmap(open.FileName);
textBox1.Text = open.FileName;
}
}
}
}