Working on the 3-tier architecture in C# ASP.net

Initially i retrieve all the asp.net attributes from the c-sharp page.

protected void Register_Click(object sender, EventArgs e)
    {
        String fname = this.firstname.Text;
        String lname = this.lastname.Text;
        String username = this.username.Text;
        String address = this.address.Text;
        String gender = this.gender.Text;
        String age = this.age.Text;
        String pwd = this.password.Text;
        String cpwd = this.cpassword.Text;
        String secQ = this.secQ.Text;
        String secA = this.secA.Text;

        UserOp userop = new UserOp();

        if (userop.signup(fname, lname, username, address, gender, age, pwd, secQ, secA))
        {
            Response.Redirect("home.aspx");
        }
        else
        {
            Response.Redirect("index.aspx");
        }


    }

And the second layer will consist of the following method

 public bool signup(String fname,String lname,String username,String address,String gender,String age,
                String password, String secQ, String secA)
    {
        user = new User(fname,lname,username,address,gender,age,password,secQ,secA);

        userdata.addUser(user);
        return true;
    }

Finally the data access layer will consist of the sql part which adds the user to the database.

 private SqlConnection con;
    private SqlDataAdapter sda;
    private SqlCommand command;
    private SqlDataReader sdr;


    public UserData()
    {
        con = new SqlConnection("Data Source=WIN-5KGBCLFK1FJ;Initial Catalog=thedb;Integrated Security=True");
        sda = new SqlDataAdapter();
    }


    public Boolean addUser(User user)
    {



       try
       {
           con.Open();
           sda.InsertCommand = new SqlCommand("INSERT INTO tbluser VALUES ('" + user.Fname + "','" + user.Lname + "','" + user.Username + "','" + user.Age + "','" + user.Sex + "','" + user.Country + "','" + user.Password + "','" + user.SecQ + "','" + user.SecA + "')", con);
           con.Open();
           sda.InsertCommand.ExecuteNonQuery();
           con.Close();
           return true;
       }
       catch (Exception ex)
       {
           throw ex;
       }

   }

Thinking it was something wrong with the SQL part i changed it to the following code but the same exception occurs.

    sda.InsertCommand = new SqlCommand ("INSERT INTO tbluser (fName,lName,username,age,sex,country, password, secQ, secA) VALUES (@fname,@lname,@username,@age,@sex,@country,@password,@secQ,@secA)",con);
        sda.InsertCommand.Parameters.Add("@fname", SqlDbType.VarChar).Value = user.Fname;
        sda.InsertCommand.Parameters.Add("@lname", SqlDbType.VarChar).Value = user.Lname;
        sda.InsertCommand.Parameters.Add("@username", SqlDbType.VarChar).Value = user.Username;
        sda.InsertCommand.Parameters.Add("@age", SqlDbType.VarChar).Value = user.Age;
        sda.InsertCommand.Parameters.Add("@sex", SqlDbType.VarChar).Value = user.Sex;
        sda.InsertCommand.Parameters.Add("@country", SqlDbType.VarChar).Value = user.Country;
        sda.InsertCommand.Parameters.Add("@password", SqlDbType.VarChar).Value = user.Password;
        sda.InsertCommand.Parameters.Add("@secQ", SqlDbType.VarChar).Value = user.SecQ;
        sda.InsertCommand.Parameters.Add("@secA", SqlDbType.VarChar).Value = user.SecA;
        con.Open();
        sda.InsertCommand.ExecuteNonQuery();
        con.Close();
        return true;

InvalidOperationException Unhanded by the User code An exception of type 'System.InvalidOperationException' occurred in DataAccessLayer.dll but was not handled in user code

The user records does not seem to be adding to the database table.

Related posts

Recent Viewed