protected void Page_Load(object sender, System.EventArgs e)
    {
        //CommonFunctions.Connection.Open ();

        MainAdapter = CommonFunctions.PrepareAdapter(CommonFunctions.GetConnection(), "SELECT * FROM Users WHERE ID = @UserID", SqlDbType.Int);

        CountriesAdapter = CommonFunctions.PrepareAdapter(CommonFunctions.GetConnection(), "SELECT DISTINCT(Country) AS Country " +
                                                          "FROM Users WHERE (IfAgent = 1) AND (LEN(Country) > 0) AND NOT (ID = @UserID)", SqlDbType.Int);

        AgentsAdapter = CommonFunctions.PrepareAdapter(CommonFunctions.GetConnection(), "SELECT -1 AS ID, 'Not referred by anyone' AS FullName " +
                                                       "UNION SELECT ID, LastName + ', ' + FirstName AS FullName FROM Users WHERE (Country = @Country) AND" +
                                                       " (IfAgent = 1) AND NOT (ID = @UserID)", SqlDbType.NVarChar, 300, SqlDbType.Int);

        IfAdmin.Visible      = AuthenticationManager.IfAdmin && (AuthenticationManager.UserID != userid);
        IfAdminLabel.Visible = AuthenticationManager.IfAdmin && (AuthenticationManager.UserID != userid);

        MainAdapter.SelectCommand.Parameters["@UserID"].Value = userid;
        //lock (CommonFunctions.Connection)
        if (MainAdapter.Fill(MainDataSet, "Users") == 0)
        {
            Response.Redirect(CommonFunctions.PrepareURL("InternalError.aspx"), true);
        }

        //CountryRequired.Enabled = (MainDataSet.Tables["Users"].Rows[0]["IfAgent"] is bool) &&
        //    (bool)MainDataSet.Tables["Users"].Rows[0]["IfAgent"];
        DBConnection obj = new DBConnection();

        try
        {
            if (!IsPostBack)
            {
                DataTable dt = VADBCommander.CountyDistinctSimpleList();
                ddlCountries.DataSource = dt;
                //ddlCountries.DataTextField = "country";
                //ddlCountries.DataValueField = "country";

                if (ddlCountries.Items.Contains(new ListItem(MainDataSet.Tables["Users"].Rows[0]["country"].ToString(),
                                                             MainDataSet.Tables["Users"].Rows[0]["country"].ToString())))
                {
                    ddlCountries.SelectedValue = MainDataSet.Tables["Users"].Rows[0]["country"].ToString();
                }
                //ddlCountries.DataBind();
            }
        }
        catch (Exception ex) { Response.Write(ex.Message); }
        finally { obj.CloseConnection(); }


        if (!IsPostBack)
        {
            EmailAddress.Text = MainDataSet.Tables["Users"].Rows[0]["Email"].ToString();
            FirstName.Text    = MainDataSet.Tables["Users"].Rows[0]["FirstName"].ToString();
            LastName.Text     = MainDataSet.Tables["Users"].Rows[0]["LastName"].ToString();
            CompanyName.Text  = MainDataSet.Tables["Users"].Rows[0]["CompanyName"].ToString();
            Address.Text      = MainDataSet.Tables["Users"].Rows[0]["Address"].ToString();
            City.Text         = MainDataSet.Tables["Users"].Rows[0]["City"].ToString();
            State.Text        = MainDataSet.Tables["Users"].Rows[0]["State"].ToString();
            Zip.Text          = MainDataSet.Tables["Users"].Rows[0]["Zip"].ToString();

            //foreach (ListItem li in ddlCountries.Items)
            //{
            //    if (li.Value.ToLower() == MainDataSet.Tables["Users"].Rows[0]["Country"].ToString().ToLower())
            //    {
            //        ddlCountries.SelectedValue = MainDataSet.Tables["Users"].Rows[0]["Country"].ToString();
            //    }
            //}

            PrimaryTelephone.Text   = MainDataSet.Tables["Users"].Rows[0]["PrimaryTelephone"].ToString();
            EveningTelephone.Text   = MainDataSet.Tables["Users"].Rows[0]["EveningTelephone"].ToString();
            DaytimeTelephone.Text   = MainDataSet.Tables["Users"].Rows[0]["DaytimeTelephone"].ToString();
            MobileTelephone.Text    = MainDataSet.Tables["Users"].Rows[0]["MobileTelephone"].ToString();
            Website.Text            = MainDataSet.Tables["Users"].Rows[0]["Website"].ToString();
            Registered.Text         = MainDataSet.Tables["Users"].Rows[0]["Registered"].ToString();
            PayTravelAgents.Checked = MainDataSet.Tables["Users"].Rows[0]["IfPayTravelAgents"] is bool?
                                      (bool)MainDataSet.Tables["Users"].Rows[0]["IfPayTravelAgents"] : false;
            ReservationSame.Checked = MainDataSet.Tables["Users"].Rows[0]["IfReservationSame"] is bool?
                                      (bool)MainDataSet.Tables["Users"].Rows[0]["IfReservationSame"] : false;
            ReservationNotSame.Checked = !ReservationSame.Checked;
            ReservationEmail.Text      = MainDataSet.Tables["Users"].Rows[0]["ReservationEmail"].ToString();
            ReservationFirstName.Text  = MainDataSet.Tables["Users"].Rows[0]["ReservationFirstName"].ToString();
            ReservationLastName.Text   = MainDataSet.Tables["Users"].Rows[0]["ReservationLastName"].ToString();
            if (IfAdmin.Visible)
            {
                IfAdmin.SelectedIndex = (MainDataSet.Tables["Users"].Rows[0]["IfAdmin"] is bool) &&
                                        (bool)MainDataSet.Tables["Users"].Rows[0]["IfAdmin"] ? 0 : 1;
            }

            ReservationSame_CheckedChanged(sender, e);

            CountriesAdapter.SelectCommand.Parameters["@UserID"].Value = userid;

            //lock (CommonFunctions.Connection)
            CountriesAdapter.Fill(MainDataSet, "Countries");

            AgentCountries.DataSource = MainDataSet;

            if (!IsPostBack)
            {
                DataBind();
            }

            if (MainDataSet.Tables["Users"].Rows[0]["ReferredByID"] is int)
            {
                object country = null;
                using (SqlConnection connection = CommonFunctions.GetConnection()) {
                    connection.Open();
                    SqlCommand getcountry = new SqlCommand("SELECT Country FROM Users WHERE ID = @UserID", connection);
                    getcountry.Parameters.Add("@UserID", SqlDbType.Int, 4);
                    getcountry.Parameters["@UserID"].Value = (int)MainDataSet.Tables["Users"].Rows[0]["ReferredByID"];

                    country = getcountry.ExecuteScalar();
                    connection.Close();
                }
                if (country is string)
                {
                    try
                    {
                        AgentCountries.SelectedValue = (string)country;
                    }
                    catch (Exception)
                    {
                    }
                }

                AgentCountries_SelectedIndexChanged(sender, e);

                try
                {
                    Agents.SelectedValue = ((int)MainDataSet.Tables["Users"].Rows[0]["ReferredByID"]).ToString();
                }
                catch (Exception)
                {
                }
            }
            else
            {
                AgentCountries_SelectedIndexChanged(sender, e);
            }
        }
    }