1

I am using 3 tier architecture with ProfileDAL and ProfileBLL. I think my code for create is fine but i always encounter this particular error.

My database name is CottonyDB.mdf. Below are the pics for the error and my web.config.

Error for database: Error for database

web.config: web.config file

Heres my code for create:

public class ProfileDAL { string strConnectionString = ConfigurationManager.ConnectionStrings["CottonyDBString"].ToString();

    public int create_Account(string Username, string pwd1, string email, string mobile, string pwdSalt, string mblVerified, string emailVerified)
    {
        var returnValue = 0;

        using (SqlConnection con = new SqlConnection(strConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO ACCOUNT (Username,PasswordHash,Email,Mobile,PasswordSalt,MobileVerified,EmailVerified) Values (@Username,@PasswordHash,@email,@mobile,@pwdSalt,@mblVerified,@emailVerified)", con))
            {
                SqlParameter[] prms = new SqlParameter[7];

                prms[0] = new SqlParameter("@Username", SqlDbType.NChar, 20);
                prms[0].Value = Username;

                prms[1] = new SqlParameter("@PasswordHash", SqlDbType.NVarChar, 20);
                prms[1].Value = pwd1;

                prms[2] = new SqlParameter("@Email", SqlDbType.NChar, 20);
                prms[2].Value = email;

                prms[3] = new SqlParameter("@Mobile", SqlDbType.NChar, 10);
                prms[3].Value = mobile;

                prms[4] = new SqlParameter("@PasswordSalt", SqlDbType.NVarChar, 20);
                prms[4].Value = pwdSalt;

                prms[5] = new SqlParameter("@MobileVerified", SqlDbType.NVarChar, 10);
                prms[5].Value = mblVerified;

                prms[6] = new SqlParameter("@EmailVerified", SqlDbType.NVarChar, 10);
                prms[6].Value = emailVerified;

                cmd.Parameters.AddRange(prms);
                con.Open();
                returnValue = cmd.ExecuteNonQuery();
                con.Close();



            }
            return returnValue;




        }




    }

}
QuinlaN
  • 13
  • 4

1 Answers1

1

Remove Initial Catalog=CottonyDB; from your connection string, it already knows which database to use because you are using AttachDbFileName.

Have a look at this SO question - Cannot attach the file *.mdf as database

Community
  • 1
  • 1
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
  • Hi, i tried deleting the Initial Catalog = CottonyDB but it still shows an error. The error states that must declare scalar variable "@pwdsalt" – QuinlaN Jul 17 '16 at 08:16
  • Yes but notice that you are getting a different error now.You can connect to the database now but you are getting an error while executing the SQL script.And the reason you're getting that error is pretty obvious - You're specifying a variable called pwdsalt in your insert statement but in the SqlParameter declaration you refer to it as PasswordSalt. – Denys Wessels Jul 17 '16 at 08:23
  • Ah i see in my VALUES statement i used the variables instead of the column names... i tried changing that and this time it goes through my DB. – QuinlaN Jul 17 '16 at 08:35
  • Thanks for the clarification Denis – QuinlaN Jul 17 '16 at 08:36