I am having trouble adding to the database. I am using a 3-tier architecture and have been struggling adding to the database.
In my Data Access Layer I have a db helper class which helps with the execution of the queries.
public static bool ExecuteNonQuery(string commandName, CommandType cmdType,
SqlParameter[] pars)
{
int result = 0;
using (SqlConnection con = new SqlConnection(connString))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = cmdType;
cmd.CommandText = commandName;
cmd.Parameters.AddRange(pars);
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
result = cmd.ExecuteNonQuery();
}
catch
{
throw;
}
}
}
return result > 0;
}
And in my dbAccess I have
public bool RegisterOfficer(Officer o)
{
SqlParameter[] pars = new SqlParameter[]
{
new SqlParameter("@Officer",o.officerID ),
new SqlParameter("@StationID",o.StationID ),
new SqlParameter("@FirstName",o.firstName ),
new SqlParameter("@LastName", o.lastName),
new SqlParameter("@Address", o.address),
new SqlParameter("@DateOfBirth", o.dateOfBirth),
new SqlParameter("@CellPhone",o.cellPhone),
new SqlParameter("@Language",o.language),
new SqlParameter("@Email", o.email),
new SqlParameter("@Rank", o.rank),
new SqlParameter("@Password", o.password),
};
return DBHelper.ExecuteNonQuery("spRegisterOfficer", CommandType.StoredProcedure, pars);
}
In my Business Logic Layer I have
public bool RegisterOfficer(Officer o)
{
return db.RegisterOfficer(o);
}
And my code for the button is like this
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Edocket"].ConnectionString);
BusinessLogicLayer bl = new BusinessLogicLayer();
Officer newOfficer = null;
protected void CreateUser_Click(object sender, EventArgs e)
{
newOfficer = new Officer();
newOfficer.officerID =Convert.ToInt64(txtIDNum.Text);
newOfficer.StationID = int.Parse(txtStation.Text);
newOfficer.firstName = txtFirstName.Text;
newOfficer.lastName = txtLastName.Text;
newOfficer.address = txtAddress.Text;
newOfficer.dateOfBirth = Convert.ToDateTime(txtDateOfBirth.Text);
newOfficer.cellPhone = txtCellPhone.Text;
newOfficer.language = txtLanguage.Text;
newOfficer.email = txtEmail.Text;
newOfficer.rank = txtRank.Text;
newOfficer.password = txtPassword.Text;
try
{
bl.RegisterOfficer(newOfficer);
Response.Redirect("Default.aspx");
}
catch (Exception)
{
}
But nothing happens after I click and the records are not being added to the database
What I have tried:
I have tried lots of googling and tracing through the code