//check UserName is exist or not and check  secquestion and answer  exits or not
 public int checkUser(DAUsers dausr)
 {
     con.Open();
     SqlCommand cmd = new SqlCommand("select count(*) from Login where Username=@Username  and SecQuestion=@SecQuestion and Answer=@Answer ", con); 
     cmd.Parameters.AddWithValue("@Username", dausr.Username);
     cmd.Parameters.AddWithValue("@SecQuestion", dausr.SecQuestion);
     cmd.Parameters.AddWithValue("@Answer", dausr.Answer);
     int countUserCheck = (int)cmd.ExecuteScalar();
     con.Close();
     return countUserCheck;
 }
 //validate the user credentials and status should be Enable
 public int LoginUserAuthentication(DAUsers dausr)
 {
     SqlCommand cmd = new SqlCommand("select count(*) from Login where Username=@Username and Password=@Password and Status=@Status", con);
     con.Open();
     cmd.Parameters.AddWithValue("@Username", dausr.Username);
     cmd.Parameters.AddWithValue("@Password", dausr.Password);
     cmd.Parameters.AddWithValue("@Status", "Enabled");
     int i = (int)cmd.ExecuteScalar();
     con.Close();
     return i;
 }
        //if three user inputs are matched with users table and password is sent  to user registerd email address.
        private void SendPasswordtoEmail(DAUsers usrs)
        {
          
            string email=usrs.GetEmailId(usrs);

            string HostAdd = ConfigurationManager.AppSettings["Host"].ToString();  //host name is  gmail or yahoo or outlook
            string FromMailId = ConfigurationManager.AppSettings["FromMail "].ToString(); //sender email addresss
            string Password = ConfigurationManager.AppSettings["Password"].ToString();  //sender email password

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(FromMailId);

            mail.Subject = "Login password";  //subject of email

            mail.Body = "Hello " + usrs.Name + Environment.NewLine + "Your Login password:"******"Click here go to Login Page"+"http://localhost:1033/Login.aspx";
            mail.IsBodyHtml = true;
            mail.To.Add(new MailAddress(email));

            SmtpClient smtp = new SmtpClient();
            smtp.Host = HostAdd;


            smtp.EnableSsl = true;
            NetworkCredential networkcred = new NetworkCredential();
            networkcred.UserName = mail.From.Address;
            networkcred.Password = Password;
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = networkcred;
            smtp.Port = 587;

            try
            {
                smtp.Send(mail);
                lblmsg.Text = "your password is send to Registerd email";
                lblmsg.ForeColor = Color.Green;
                ClearControlsData(Page.Controls);
               
            }
            catch(Exception ex)
            {
                
                lblmsg.Text = "your password is not sent to registerd email address";
                lblmsg.ForeColor = Color.Red;
                ClearControlsData(Page.Controls);
            }


            
        }
  //insert the user details in Login Table
  public int insertUserDetails(DAUsers dausrs)
 {
    
    SqlCommand cmd = new SqlCommand("insert into Login(LoginId,Name,Phno,email,Username,Password,SecQuestion,Answer) values(@LoginId,@Name,@Phno,@email,@Username,@Password,@SecQuestion,@Answer)", con);
    cmd.Parameters.AddWithValue("@LoginId",(int)dausrs.LoginId);
    cmd.Parameters.AddWithValue("@Name",dausrs.Name);
      cmd.Parameters.AddWithValue("@Phno",dausrs.Phno);
      cmd.Parameters.AddWithValue("@email", dausrs.Email);
      cmd.Parameters.AddWithValue("@Username", dausrs.Username);
      cmd.Parameters.AddWithValue("@Password", dausrs.Password);
      cmd.Parameters.AddWithValue("@SecQuestion", dausrs.SecQuestion);
      cmd.Parameters.AddWithValue("@Answer", dausrs.Answer);
      con.Open();
      int i = cmd.ExecuteNonQuery();
      con.Close();
      return i;
 }
        //Retrive the password from table based on username,security question and answer.
      
        public string GetPassword(DAUsers dausr)
        {
            SqlCommand cmd = new SqlCommand("Select Password from Login where Username=@Username and SecQuestion=@SecQuestion and Answer=@Answer", con);
           con.Open();
            cmd.Parameters.AddWithValue("@Username", dausr.Username);
            cmd.Parameters.AddWithValue("SecQuestion", dausr.SecQuestion);
            cmd.Parameters.AddWithValue("@Answer", Answer);
            SqlDataReader dr = cmd.ExecuteReader();
            string password="";
                if (dr.Read())
                    password = dr[0].ToString();
            con.Close();
            return password;

        }
        public int GetLoginId(DAUsers dausr)
        {
            con.Open();

            SqlCommand cmd = new SqlCommand("Select LoginId from Login where Username=@Username", con);
            cmd.Parameters.AddWithValue("@Username", dausr.Username);

            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
                LoginId =Convert.ToInt32(dr[0]);

            con.Close();

            return LoginId;
        }
        //retrive the imageapath from users table
        public string getImage(DAUsers dausr)
        {
            con.Open();

            SqlCommand cmd = new SqlCommand("Select PhotoPath from Login where Username=@Username", con);
            cmd.Parameters.AddWithValue("@Username", dausr.Username);

            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
                Imagepath = dr[0].ToString();

            con.Close();
            
            return Imagepath;
        }
        //Retrive email address from users table
        public string GetEmailId(DAUsers dausr)
        {

            string EmailId="";

            con.Open();

            SqlCommand cmd = new SqlCommand("Select email from Login where Username=@Username", con);
            cmd.Parameters.AddWithValue("@Username", dausr.Username);
          
            SqlDataReader dr = cmd.ExecuteReader();

                if (dr.Read())
                    EmailId = dr[0].ToString();

             con.Close();

             return EmailId;
        }