Exemplo n.º 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckAccount();

            if (!string.IsNullOrEmpty(Request["id"]))
            {
                _id = Int32.Parse(Request["id"]);
                phAddRecord.Visible  = false;
                phEditRecord.Visible = true;
            }
            App_Code.Base db = new App_Code.Base(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
            _data = db.GetEnviromentAddresses();
            if (!IsPostBack)
            {
                AddAddress.DataSource = _data;
                AddAddress.DataBind();
                EditAddress.DataSource = _data;
                EditAddress.DataBind();
                if (_data.Count > 0)
                {
                    AddAddress.SelectedIndex  = 0;
                    EditAddress.SelectedIndex = 0;
                }
                List <string> list = db.GetEnviromentType();
                AddType.DataSource = list;
                AddType.DataBind();
                AddType.SelectedIndex = 0;
                EditType.DataSource   = list;
                EditType.DataBind();
                EditType.SelectedIndex = 0;
                if (_id > 0)
                {
                    using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()))
                    {
                        conn.Open();
                        SqlCommand cmd = new SqlCommand("e.[Name], e.Type, ea.Address, e.[Description] from [Enviroment] e " +
                                                        "join EnviromentAddresses ea on ea.Id=e.AddressId where e.Id=@id", conn);
                        cmd.Parameters.AddWithValue("id", _id);
                        SqlDataAdapter da = new SqlDataAdapter(cmd);
                        DataTable      dt = new DataTable();
                        da.Fill(dt);
                        if (dt.Rows.Count > 0)
                        {
                            EditType.SelectedValue    = dt.Rows[0]["Type"].ToString();
                            EditName.Text             = dt.Rows[0]["Name"].ToString();
                            EditAddress.SelectedValue = dt.Rows[0]["Address"].ToString();
                            EditDescription.Text      = dt.Rows[0]["Description"].ToString();
                        }
                    }
                }
            }
        }