Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Comments
PIEBALDconsult 27-Aug-17 19:42pm    
Well, maybe stop swallowing exceptions and see what happens.
KevinClaassens 27-Aug-17 20:00pm    
It is now saying that it expects a parameter @OfficerID but as far as I can see I provided all the necessary parameters
PIEBALDconsult 27-Aug-17 20:06pm    
"@Officer",o.officerID <== Not so much.
KevinClaassens 27-Aug-17 20:17pm    
How do I go about fixing it?
PIEBALDconsult 27-Aug-17 20:24pm    
Correct the parameter name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900