private static FacebookUser createUserFromDynamicUser(dynamic i_DynamicUser, FacebookUser i_User)
        {
            i_User.Id = i_DynamicUser.uid;
            i_User.FullName = i_DynamicUser.name;
            i_User.FirstName = i_DynamicUser.first_name;
            i_User.LastName = i_DynamicUser.last_name;
            i_User.Birthday = i_DynamicUser.birthday_date;

            Dictionary<string, string> tempFriendPics = new Dictionary<string, string>();
            tempFriendPics[ePictureTypes.pic_small.ToString()] = i_DynamicUser.pic_small;
            tempFriendPics[ePictureTypes.pic_big.ToString()] = i_DynamicUser.pic_big;
            tempFriendPics[ePictureTypes.pic_square.ToString()] = i_DynamicUser.pic_square;
            tempFriendPics[ePictureTypes.pic.ToString()] = i_DynamicUser.pic;
            i_User.Pictures = tempFriendPics;

            if (i_User is ApplicationUser)
            {
                (i_User as ApplicationUser).Email = i_DynamicUser.email;
            }

            return i_User;
        }
        /// <summary>
        /// returns the values for a given user and a table in INSERT syntex
        /// </summary>
        /// <param name="i_Table"></param>
        /// <param name="i_FacebookUser"></param>
        /// <returns></returns>
        private string getValuesOfTableForUser(eTabelsInDataBase i_Table, FacebookUser i_FacebookUser)
        {
            string result = null;
            if (i_Table == eTabelsInDataBase.FacebookUser)
            {
                result = string.Format("{0}, '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}'",
                    i_FacebookUser.Id,
                    i_FacebookUser.FirstName,
                    i_FacebookUser.LastName,
                    i_FacebookUser.FullName,
                    i_FacebookUser.Pictures[ePictureTypes.pic.ToString()],
                    i_FacebookUser.Pictures[ePictureTypes.pic_big.ToString()],
                    i_FacebookUser.Pictures[ePictureTypes.pic_small.ToString()],
                    i_FacebookUser.Pictures[ePictureTypes.pic_square.ToString()],
                    i_FacebookUser.BirthdayDateTime.ToShortDateString());
            }

            if (i_Table == eTabelsInDataBase.ApplicationUser)
            {
                ApplicationUser tempUser = i_FacebookUser as ApplicationUser;
                result = string.Format("{0}, '{1}', '{2}', '{3}'",
                tempUser.Id,
                tempUser.Email,
                tempUser.RegistrationDate,
                tempUser.AccessToken);
            }

            if (i_Table == eTabelsInDataBase.Friend)
            {
                Friend tempUser = i_FacebookUser as Friend;
                result = string.Format("{0}",
                    tempUser.Id);
            }

            if (i_Table == eTabelsInDataBase.Birthday_Messages)
            {
                Friend tempUser = i_FacebookUser as Friend;
                result = string.Format("'{0}', {1}",
                    tempUser.BirthdayMessage,
                    tempUser.Id);
            }

            return result;
        }
 private string getInsertCommandForFriendIntoDataBase(FacebookUser i_FacebookUser)
 {
     return string.Format(
                         "INSERT INTO {0} VALUES ({1})",
                         eTabelsInDataBase.Friend.ToString(),
                         getValuesOfTableForUser(eTabelsInDataBase.Friend, i_FacebookUser));
 }
 private string getInsertCommandForBirthdayMessageIntoDataBase(FacebookUser i_FacebookUser, ApplicationUser i_ApplicationUser)
 {
     return string.Format(
                         "INSERT INTO {0} VALUES ({1}, {2})",
                         eTabelsInDataBase.Birthday_Messages.ToString(),
                         getValuesOfTableForUser(eTabelsInDataBase.Birthday_Messages, i_FacebookUser),
                         i_ApplicationUser.Id);
 }
        /// <summary>
        /// checks if the user is in the data base without connecting to data base
        /// </summary>
        /// <param name="i_FacebookUser"></param>
        /// <returns></returns>
        private bool checkIfUserIsInDataBaseWithOutConnection(FacebookUser i_FacebookUser)
        {
            bool result = false;
            SqlDataReader dbReader = null;

            if (i_FacebookUser is ApplicationUser)
            {
                dbReader = commandDataBase(string.Format
                  ("SELECT * FROM {0} WHERE {1} = {2} ",
                  eTabelsInDataBase.ApplicationUser.ToString(),
                  eTableApplicationUser_Columns.Application_User_ID.ToString(),
                  i_FacebookUser.Id), true);
            }
            else
            {
                if (i_FacebookUser is Friend)
                {
                    //facebook user is friend
                    dbReader = commandDataBase(string.Format
                      ("SELECT * FROM {0} WHERE {1} = {2} ",
                      eTabelsInDataBase.Friend.ToString(),
                      eTable_Friend.Friend_ID.ToString(),
                      i_FacebookUser.Id), true);
                }
                else
                {
                    //only facebook user
                    dbReader = commandDataBase(string.Format
                      ("SELECT * FROM {0} WHERE {1} = {2} ",
                      eTabelsInDataBase.FacebookUser.ToString(),
                      eTable_FacebookUser.ID.ToString(),
                      i_FacebookUser.Id), true);
                }
            }

            if (dbReader.HasRows)
            {
                result = true;
            }

            closeDataBaseReader(dbReader);
            return result;
        }
 /// <summary>
 /// this method returns true if the facebook user is in the database
 /// </summary>
 /// <param name="i_FacebookUser"></param>
 /// <returns></returns>
 public bool IsUserInDataBase(FacebookUser i_FacebookUser)
 {
     openConnection();
     bool result = checkIfUserIsInDataBaseWithOutConnection(i_FacebookUser);
     closeConnection();
     return result;
 }
        /// <summary>
        /// this methods inserts a list of friends into the db.
        /// will only work if the application user is already in db.
        /// </summary>
        /// <param name="i_ApplicationUser"></param>
        /// <param name="i_FriendsToInsert"></param>
        public void InsertFriendsIntoDataBase(ApplicationUser i_ApplicationUser, ICollection<Friend> i_FriendsToInsert)
        {
            string[] insertFacebookUsersCommands = new string[i_FriendsToInsert.Count];
            string[] insertFriendsCommands = new string[i_FriendsToInsert.Count];
            string[] insertBirthdayMessageCommands = new string[i_FriendsToInsert.Count];
            int i = 0;

            //building queries foreach friend
            foreach (Friend friend in i_FriendsToInsert)
            {
                insertFacebookUsersCommands[i] = (getInsertCommandForFacebookUserInDataBase(friend));
                insertFriendsCommands[i] = (getInsertCommandForFriendIntoDataBase(friend));
                insertBirthdayMessageCommands[i] = (getInsertCommandForBirthdayMessageIntoDataBase(friend, i_ApplicationUser));
                i++;
            }

            openConnection();

            i = 0;
            //inserting users
            FacebookUser tempFacebookUser = new FacebookUser();
            foreach (Friend friend in i_FriendsToInsert)
            {

                //check if the facebook user isn't in db
                tempFacebookUser.Id = friend.Id;
                if (!checkIfUserIsInDataBaseWithOutConnection(tempFacebookUser))
                {
                    commandDataBase(insertFacebookUsersCommands[i], false);
                }

                //check if the friend isn't in db
                if (!checkIfUserIsInDataBaseWithOutConnection(friend))
                {
                    commandDataBase(insertFriendsCommands[i], false);
                }

                //check if message isn't in db
                if (!messageIsInDatabaseWithoutConnection(i_ApplicationUser, friend))
                {
                    commandDataBase(insertBirthdayMessageCommands[i], false);
                }
                i++;
            }

            closeConnection();
        }