FK NOT NULL generates error in Entity class

Posts   
 
    
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 01-May-2007 14:50:23   

Hi, Using VB2005, LLBLGen V2, Adapter, SQL Server 2K & WinForms. Seemed trivial for a while but now driving me nuts!

I have table Organisation which is related via its FK (OrgTypeID) to table OrganisationType.

My WinForm has a datagridview with 4 columns for editing table Organisation. One of the columns (OrgTypeID) is a combo box for selecting organisation name from table OrganisationType. The grid is bound via its BindingSource to table Organisation. The combo box is sourced from table OrganisationType which is populated with data.

Scenario 1 (Use an entity OrganisationType as source for combo) When column Organisation.OrgTypeID is defined as NULL my form is fully functional. i.e The form will display without errors, even if the Organisation table is empty.

When column Organisation.OrgTypeID is defined as NOT NULL (my preference) and the Organisation table is empty, this causes an issue in in the classes OrganisationTypeEntity and OrganisationEntity and a 'System.ArgumentException: Value is not Valid' error then shows up in the grid. Similarly, an error is generated on a blank row when the table has data.

Question 1: As I would like to define OrgTypeID as NOT NULL, what do I have to do to get rid of the error ?

Scenario 2 (Use a TypedList as source for Combo) I create a TypedList from table OrganisationType and drag it from the Toolbox for use on my form (via another BindingSource) as the data source for the combo box.

Problem: Unlike scenario 1, I can't get the combo box to display when I run and open the form. Is there anything wrong with my code for filling the TypedList?

    Function GetSortedOrganisationTypeList() As OrganisationTypeListTypedList
        Dim adapter As New DataAccessAdapter
        Try
            Dim orgTypeList As New OrganisationTypeListTypedList
            Dim sorter As ISortExpression = New SortExpression()
            sorter.Add(New SortClause(OrganisationTypeFields.OrgTypeName, Nothing, SortOperator.Ascending))
            adapter.FetchTypedList(orgTypeList.GetFieldsInfo(), orgTypeList, Nothing, 0, sorter, True)
            Return orgTypeList
        Finally
            adapter.Dispose()
        End Try
    End Function

Thanks

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 01-May-2007 15:43:51   

Hello,

if you defined a fk to NOT NULL, you need to have the table OrganisationType not empty else you cannot link the organisation to the organisation type. In your scenarion 2, did you haver any error or did the result is empty?

Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 01-May-2007 16:56:13   

Hi,

jbb wrote:

if you defined a fk to NOT NULL, you need to have the table OrganisationType not empty else you cannot link the organisation to the organisation type.

Table OrganisationType is a lookup table and is fully populated to maintain ref/integrity, hence my puzzlement.

In your scenarion 2, did you have any error or did the result is empty?

The combox box doesn't appear at its position, then when I click on the form I get the System.ArgumentException error and the combo box appears in its column but on the line bbelow, the blank new row line. I then have to Stop Debugging. confused

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 02-May-2007 09:51:12   

Scenario #1: Would you please post the binding code of the comboBox and the grid?

The comboBox binding should look like the following:

<asp :DropDownList ID="any" runat="server" DataSourceID="DS_OrgTypes" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("ItemCategoryId") %>'/>

The selected value should be binded to the FK field of the Organization entity.

Sceanrio #2: Please examine the generated SQL query, run it directly against the db and check if it returns any rows.

Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 03-May-2007 19:48:17   

Hello Walaa,

Scenario #1: Would you please post the binding code of the comboBox and the grid?

The comboBox binding should look like the following:

<asp :DropDownList ID="any" runat="server" DataSourceID="DS_OrgTypes" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("ItemCategoryId") %>'/>

The selected value should be binded to the FK field of the Organization entity.

Its a WinForm app, so have got this from the designer. Not sure if this is what you want Combobox

        Me.OrgTypeID.DataPropertyName = "OrgTypeID"
        Me.OrgTypeID.DataSource = Me.BindingSource2
        Me.OrgTypeID.DisplayMember = "OrgTypeName"
        Me.OrgTypeID.HeaderText = "Type"
        Me.OrgTypeID.Name = "OrgTypeID"
        Me.OrgTypeID.Resizable = System.Windows.Forms.DataGridViewTriState.[True]
        Me.OrgTypeID.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic
        Me.OrgTypeID.ValueMember = "OrgTypeID"
        Me.OrgTypeID.Width = 170

DataGridView

        Me.DataGridView1.AllowUserToResizeColumns = False
        Me.DataGridView1.AllowUserToResizeRows = False
        DataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(CType(CType(224, Byte), Integer), CType(CType(224, Byte), Integer), CType(CType(224, Byte), Integer))
        Me.DataGridView1.AlternatingRowsDefaultCellStyle = DataGridViewCellStyle1
        Me.DataGridView1.AutoGenerateColumns = False
        Me.DataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.DataGridView1.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.[Single]
        DataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.BottomLeft
        DataGridViewCellStyle2.BackColor = System.Drawing.SystemColors.Control
        DataGridViewCellStyle2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        DataGridViewCellStyle2.ForeColor = System.Drawing.SystemColors.WindowText
        DataGridViewCellStyle2.SelectionBackColor = System.Drawing.SystemColors.Highlight
        DataGridViewCellStyle2.SelectionForeColor = System.Drawing.SystemColors.HighlightText
        DataGridViewCellStyle2.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
        Me.DataGridView1.ColumnHeadersDefaultCellStyle = DataGridViewCellStyle2
        Me.DataGridView1.ColumnHeadersHeight = 27
        Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.DisableResizing
        Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.OrgIDDataGridViewTextBoxColumn, Me.OrgAcronymDataGridViewTextBoxColumn, Me.OrgNameDataGridViewTextBoxColumn, Me.OrgTypeID})
        Me.DataGridView1.DataSource = Me.BindingSource1
        Me.DataGridView1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.DataGridView1.Location = New System.Drawing.Point(0, 0)
        Me.DataGridView1.MultiSelect = False
        Me.DataGridView1.Name = "DataGridView1"
        Me.DataGridView1.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.[Single]
        Me.DataGridView1.RowHeadersWidth = 22
        Me.DataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing
        Me.DataGridView1.RowTemplate.DefaultCellStyle.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.DataGridView1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.DataGridView1.Size = New System.Drawing.Size(758, 472)
        Me.DataGridView1.TabIndex = 1

Scenario #2: Please examine the generated SQL query, run it directly against the db and check if it returns any rows.

Sorry, but how do you get hold of the generated SQL?

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-May-2007 00:28:44   

Sorry, but how do you get hold of the generated SQL?

Please read LLBLGenPro Help - Using generated code - Troubleshooting and debugging

David Elizondo | LLBLGen Support Team
Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 07-May-2007 17:34:49   

Hi,

for scenario 2, can you place a breakpoint at the end of the typedlist returning function to see what you get there?

for scenario 1, I think we don't have the part concerning the inner combobox in your sample. Can you check that?

Also, i think you should be able to find some more about the source of the exception by debugging/tracing. Can you try and paste some related code in there?

Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 09-May-2007 14:24:26   

Thanks guys. I'm still away but will check those suggestions out and respond in a couple of days.

simple_smile