コード例 #1
0
ファイル: AddEdit.aspx.cs プロジェクト: vsrz/CS498
    private void LoadData()
    {
        // Find the primary key property
        PropertyInfo primaryKey = GetPrimaryKeyProperty(DataType);

        if (ItemId != null)
        {

            object itemToCreateOrEdit = new object();
            MonkData db = new MonkData();
            object[] queryParams = { };
            string tableName = ((System.Data.Linq.Mapping.TableAttribute)DataType.GetCustomAttributes(typeof(System.Data.Linq.Mapping.TableAttribute), false).First()).Name;
            // Load the object from the database.
            IEnumerable itemsFromDB = db.ExecuteQuery(DataType, "select * from " + tableName + " where " + primaryKey.Name + " = '" + ItemId.ToString() + "'", queryParams);
            foreach (object item in itemsFromDB)
            {
                itemToCreateOrEdit = item;
            }

            foreach (PropertyInfo prop in itemToCreateOrEdit.GetType().GetProperties())
            {
                foreach (Control cntl in plcForm.Controls)
                {
                    if (cntl.ID == prop.Name) // Look to see if this is the property
                    {
                        if (cntl.GetType() == typeof(TextBox))
                        {
                            TextBox txtControl = (TextBox)cntl;
                            if (prop.GetValue(itemToCreateOrEdit, null) != null)
                                txtControl.Text = prop.GetValue(itemToCreateOrEdit, null).ToString();
                        }
                        else if (cntl.GetType() == typeof(FreeTextBox))
                        {
                            FreeTextBox txtControl = (FreeTextBox)cntl;
                            if (prop.GetValue(itemToCreateOrEdit, null) != null)
                                txtControl.Text = prop.GetValue(itemToCreateOrEdit, null).ToString();
                        }
                        else if (cntl.GetType() == typeof(CheckBox))
                        {
                            CheckBox chkBox = (CheckBox)cntl;
                            if (prop.GetValue(itemToCreateOrEdit, null) != null)
                                chkBox.Checked = (bool)prop.GetValue(itemToCreateOrEdit, null);
                        }
                        else if (cntl.GetType() == typeof(DropDownList))
                        {
                            DropDownList dList = (DropDownList)cntl;

                            PropertyInfo primaryKeyOfReferencedTable = GetPrimaryKeyProperty(prop.PropertyType);

                            if (prop.GetValue(itemToCreateOrEdit, null) != null)
                            {
                                ListItem itemToSelect = dList.Items.FindByValue(primaryKeyOfReferencedTable.GetValue(prop.GetValue(itemToCreateOrEdit, null), null).ToString());
                                if (itemToSelect == null)
                                {
                                    ListItem nullListItem = dList.Items.FindByValue("null");
                                    if (nullListItem != null)
                                        nullListItem.Selected = true;
                                }
                                else
                                    itemToSelect.Selected = true;
                            }
                            else
                            {

                                ListItem itemToSelect = dList.Items.FindByValue("null");
                                if (itemToSelect != null)
                                    itemToSelect.Selected = true;
                            }
                        }
                    }
                }

            }
        }
    }
コード例 #2
0
ファイル: AddEdit.aspx.cs プロジェクト: vsrz/CS498
    public void btnSave_Clicked(object sender, EventArgs e)
    {
        object itemToCreateOrEdit;
        MonkData db = new MonkData();

        // Find the primary key property
        PropertyInfo primaryKey = GetPrimaryKeyProperty(DataType);

        if (ItemId == null)
        {
            // Dynamically get the datatype
            Type[] emptyConstructorArgs = { };
            itemToCreateOrEdit = DataType.GetConstructor(emptyConstructorArgs).Invoke(null);
        }
        else
        {
            // Load the object from the database.

            Type[] emptyConstructorArgs = { };
            string tableName = ((System.Data.Linq.Mapping.TableAttribute)DataType.GetCustomAttributes(typeof(System.Data.Linq.Mapping.TableAttribute), false).First()).Name;

            IEnumerable itemsFromDB = db.ExecuteQuery(DataType, "select * from " + tableName + " where " + primaryKey.Name + " = '" + ItemId.ToString() + "'", emptyConstructorArgs);
            IEnumerator dbItemsEnumberator = itemsFromDB.GetEnumerator();
            dbItemsEnumberator.MoveNext();
            itemToCreateOrEdit = dbItemsEnumberator.Current;
        }

        // Load all the values from the form into the object
        foreach (Control cntItem in plcForm.Controls)
        {
            var propertiesFound = from p in DataType.GetProperties()
                                  where p.Name == cntItem.ID
                                  select p;
            if (propertiesFound.Count() < 1)
                continue;
            PropertyInfo pInfo = propertiesFound.First();
            object[] attributesOfProperty = pInfo.GetCustomAttributes(false);
            if (attributesOfProperty[0].GetType() == typeof(ColumnAttribute))
            {
                ColumnAttribute columnAttrib = (ColumnAttribute)attributesOfProperty[0];
                if (columnAttrib.DbType.Contains("VarChar") || columnAttrib.DbType.Contains("NChar"))
                {
                    TextBox txtControl = (TextBox)cntItem;
                    pInfo.SetValue(itemToCreateOrEdit, txtControl.Text, null);
                }
                else if (columnAttrib.DbType.Contains("Decimal"))
                {
                    TextBox txtControl = (TextBox)cntItem;
                    pInfo.SetValue(itemToCreateOrEdit, decimal.Parse(txtControl.Text), null);
                }
                else if (columnAttrib.DbType.Contains("DateTime"))
                {
                    TextBox txtControl = (TextBox)cntItem;
                    if (!String.IsNullOrEmpty(txtControl.Text))
                    {
                        DateTime time;

                        // Check if the datetime supplied is actually parseable.
                        if(!DateTime.TryParse(txtControl.Text, out time))
                        {
                            throw new Exception("Failed to parse the datetime in the textbox to the proper datetime format. An example is 12/31/2008 12:59:59 AM. The field was " + GetFieldNameFromString(pInfo.Name));
                        }

                        pInfo.SetValue(itemToCreateOrEdit, time, null);
                    }
                    else if (columnAttrib.CanBeNull)
                        pInfo.SetValue(itemToCreateOrEdit, null, null);
                    else if (!columnAttrib.CanBeNull)
                        throw new Exception("Tried to insert a null datetime into a field tha required a datetime. Field was " + GetFieldNameFromString(pInfo.Name));
                }
                else if (columnAttrib.DbType.Contains("Text"))
                {
                    FreeTextBox txtControl = (FreeTextBox)cntItem;
                    pInfo.SetValue(itemToCreateOrEdit, txtControl.Text, null);
                }
                else if (columnAttrib.DbType.Contains("Bit"))
                {
                    CheckBox chkBox = (CheckBox)cntItem;
                    pInfo.SetValue(itemToCreateOrEdit, chkBox.Checked, null);
                }
                else if (columnAttrib.DbType.Contains("Int"))
                {
                    TextBox txtControl = (TextBox)cntItem;

                    int parsedValue = 0;
                    if(!int.TryParse(txtControl.Text, out parsedValue))
                    {
                        throw new Exception("Failed converting the text in the textbox to an integer. The property name was " + GetFieldNameFromString(pInfo.Name));
                    }

                    pInfo.SetValue(itemToCreateOrEdit, parsedValue, null);
                }
            }
            else if (attributesOfProperty[0].GetType() == typeof(AssociationAttribute))
            {
                AssociationAttribute assocAttrib = (AssociationAttribute)attributesOfProperty[0];

                if (cntItem.GetType() == typeof(DropDownList))
                {
                    DropDownList dList = (DropDownList)cntItem;
                    PropertyInfo propIDOfOtherTableItem = DataType.GetProperties().First(p => p.Name == assocAttrib.ThisKey);
                    if (dList.SelectedValue == "null" || dList.SelectedValue == "")
                    {
                        propIDOfOtherTableItem.SetValue(itemToCreateOrEdit, null, null);
                    }
                    else
                    {
                        Guid selectedGuidOfItem = new Guid(dList.SelectedValue);
                        propIDOfOtherTableItem.SetValue(itemToCreateOrEdit, selectedGuidOfItem, null);
                    }
                }
            }

        }
        // Save the object...

        if (ItemId == null)
        {
            if (primaryKey.PropertyType != typeof(Guid))
                throw new Exception("Primary key of " + DataType.Name + " is not a Guid.");
            primaryKey.SetValue(itemToCreateOrEdit, Guid.NewGuid(), null);

            object[] itemToInsert = { itemToCreateOrEdit };
            //                           Where finds the table we need to examine    // Find the one that's shortest   //Get prop vlaue     // Get the method to insert  // Insert
            var propertyInfosForDatabaseWhereName = db.GetType().GetProperties().Where(p => p.Name.StartsWith(DataType.Name.Substring(0, DataType.Name.Length - 1)));
            var propertyObjectOfTableFromDB = propertyInfosForDatabaseWhereName.OrderBy(p => p.Name.Length).First().GetValue(db, null);
            propertyObjectOfTableFromDB.GetType().GetMethod("InsertOnSubmit").Invoke(propertyObjectOfTableFromDB, itemToInsert);

        }

        db.SubmitChanges();
        mvAddEdit.ActiveViewIndex = 1;

        hlViewItem.NavigateUrl = "AddEdit.aspx?typename=" + DataType.Name + "&itemid=" + primaryKey.GetValue(itemToCreateOrEdit, null).ToString();
        hlReturnToList.NavigateUrl = "GridView.aspx?typename=" + DataType.Name;
    }