WebGrid Community Server

Welcome to WebGrid Community Server Sign in | Join | Help
in Search

Setting default values for hidden columns

Last post 08-28-2008, 7:31 by matt_chrs. 1 replies.
Sort Posts: Previous Next
  •  08-18-2008, 16:54 739

    Setting default values for hidden columns

    I have a database table that is used for two types of objects: Updates and Articles.  Updates use a subset of the fields in Articles.  So, I have a WebGrid on my Updates page that is bound to the database table and I'm hiding all the fields that do not apply to the Update object or cannot be changed (e.g. the status is always set to "Pending" for new records).

    To do this, I'm implementing the NewRecordClick event.  In this event, I'm setting the default values for the hidden columns.  Some of the columns are foreign keys and they do not always seem to "remember" their values when the Grid attempts to insert the record into the table.

    Sometimes I can fix the problem by commenting out the code in the NewRecordClick event, running the website, stopping the application, uncommenting out the code, running it again, and it works with no change to the actual code logic.

    Has anyone else experienced anything like this?  Am I not setting something properly for the foreign key columns?

    Here's my code for the event:

    protected void pageGrid_NewRecordClick(object sender, WebGrid.Events.NewRecordClickEventArgs e)
    {
        // Cast columns to "Foreignkey" objects so the "ValueId" property can
        // be set, which is the property used when generating the INSERT statement
        WebGrid.Columns.Foreignkey fkColUser = ((WebGrid.Columns.Foreignkey)pageGrid.Columns["ART_USER_ID"]);
        WebGrid.Columns.Foreignkey fkColStatus = ((WebGrid.Columns.Foreignkey)pageGrid.Columns["ART_STATUS_ID"]);

        ///////////////////////////////////////////////////
        // Set default values for hidden columns here
        ///////////////////////////////////////////////////

        // User - this is a foreign key, so set the Value & ValueId properties
        Extranet MasterPage = ((Extranet)Master);
        int userId = MasterPage.GetUser();
        CssUser user = new CssUser();
        user.Select(userId);
        fkColUser.Value = user.Username;
        fkColUser.ValueId = userId.ToString();

        // Status - this is a foreign key, so set the Value & ValueId properties
        fkColStatus.Value = CssArticleStatus.PendingDescription;
        fkColStatus.ValueId = CssArticleStatus.Pending.ToString();

        // Dates
        string dateUpdated = DateTime.Now.ToString(pageGrid.DefaultDateTimeFormat);
        pageGrid.Columns["ART_CREATED"].Value = dateUpdated;
        pageGrid.Columns["ART_LAST_UPDATED"].Value = dateUpdated;
        pageGrid.Columns["ART_RELEASE_DATE"].Value = dateUpdated;
    }

    ==============================================================

    Here's the error text I get: "Cannot insert NULL value into column ART_STATUS_ID"

    You can see I'm setting the value above with the "fkColStatus" variable above, but it doesn't seem to consistently remember that value.

    Thanks for any help you can provide.

  •  08-28-2008, 7:31 752 in reply to 739

    Re: Setting default values for hidden columns

    Well to take the grid out of the picture for a minute.  If you have a default value on the tables, it should not be enforced through the grid.  Try a default column constraint or in the event that fails an insert trigger in the database layer. 

    In this way the database remains valid regardless of ui issues.

    Matthew

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems