adding row to listview raises exception


Author
Message
Ian Hammond
Ian Hammond
StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)
Group: Forum Members
Posts: 57, Visits: 277
In this particular instance I am using a listview control to display user names that can access my application. There are 2 custom fields whose content is fetched from another table. I have the listview bound to a business object which reads my usernamesBO. Each user has an entry in a userprofiles table which contains a boolean flag saying if they are logged into the application and the name of the computer they are using.

My listview displays the username, description, Computer Name, Logged In

There is a toolbar with Save Cancel Add Edit Delete

When the user click Add or Edit a child form is displayed allowing to make changes to the user profile. The BO is passed as an argument to the child form. If the user Add's a new profile, I execute a BO.NewRow() followed by assigning data to the fields. The ComputerName and LoggedIn are initialised to default values. The form is disposed an the user is returned to the parent form which excutes a listView.Requery(). Here, I am trying to display the new row.

 The Requery causes the following event to be invoked:



// Populate computer name and logged-in status

private void listViewUserNames_RowPopulating(MicroFour.StrataFrame.UI.Windows.Forms.RowPopulatingEventArgs e)

{

    HLSUserNamesBO loBO= (HLSUserNamesBO)e.BusinessObject;

    if (this.hlsUserProfilesBO1.Seek("userId = " + loBO.userId.ToString()))

    {

        e.Values[2].DisplayValue =
this.hlsUserProfilesBO1.computerName;

        e.Values[3].DisplayValue = (
Boolean)this.hlsUserProfilesBO1.loggedIn ? Global.RetrieveTextValue("Text_Yes") :            Global.RetrieveTextValue("Text_No");

    }

    else

    {

        e.Values[2].DisplayValue =
"";

        e.Values[3].DisplayValue =
Global.RetrieveTextValue("Text_No");

    }

}



This code works fine when the form is initially loaded but when I return from the Add command, the section LoggedIn section causes the exception to be raised. On load the first Id =1 and this loads ok, the second record load ok. When I return from Add the first Id =1 and it fails.

I'm not sure why this is.

Can anyone priovide an explanation or solution?

Many thanks
Trent Taylor
Trent Taylor
StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
What was the stack trace of the error that you received?
Ian Hammond
Ian Hammond
StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)
Group: Forum Members
Posts: 57, Visits: 277
Hi Trent,

Hope the following is meaningful, Regards

System.Reflection.TargetInvocationException was unhandled by user code
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
       at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
       at MicroFour.StrataFrame.Business.ReflectionPropertyDescriptor.GetValue(Object component)
       at MicroFour.StrataFrame.Business.BusinessLayer.get_Item(String FieldName)
       at MicroFour.StrataFrame.UI.Windows.Forms.ListView.CreateListViewItem(BusinessLayer BusinessObject)
       at MicroFour.StrataFrame.UI.Windows.Forms.ListView.PopulateListView(Object[] Parameters)
       at MicroFour.StrataFrame.UI.Windows.Forms.ListView.Requery()
       at Greenfield.HLSPro.Forms.UserNamesForm.cmdAdd_Click(Object sender, EventArgs e) in D:\HLS v5 Development\HLS v5\HLS v5\Forms\UserNamesForm.cs:line 56
       at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
       at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
       at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
       at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
       at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ToolStrip.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
       at System.Windows.Forms.Nativewindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  InnerException: System.InvalidCastException
       Message=Specified cast is not valid.
       Source=HLS v5
       StackTrace:
            at Greenfield.HLSPro.Business_Objects.HLSUserNamesBO.get_loggedIn() in D:\HLS v5 Development\HLS v5\HLS v5\Business Objects\HLSUserNamesBO.cs:line 148
       InnerException:
Trent Taylor
Trent Taylor
StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Well, it is telling you that on line 56 of the class code that you provided, there is an invalid cast exception.  In other words, something is trying to be converted, illegally, from one data type to another.  Debug your code and put a break point on line 56 to evaluate what the contents are of the value being stuffed into the display.  This should clue you in as to where the problem is occurring.
Edited 13 Years Ago by Trent L. Taylor
Ian Hammond
Ian Hammond
StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)
Group: Forum Members
Posts: 57, Visits: 277
Hi,

I had already traced the code. What I found on initial load was that the usernameBO was populated with values for Computername and Logged_In, but, return from the Add operation these 2 fields were null, hence the exception, but I don't quite understand why this is so. Where I assign a value to e.Values[3] it goes bombing off to the usernamesBO  into the PropertyDescriptors method of the usernamesBO until it finally generates the exception.

Regards
Trent Taylor
Trent Taylor
StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Can you shoot me a screentshot of your PopulationDataSourceSettings dialog?  This might help as well.
Ian Hammond
Ian Hammond
StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)
Group: Forum Members
Posts: 57, Visits: 277
Hi Trent,

Please find attachment, as requested.

Regards
Attachments
ListViewPopSettings.jpg (128 views, 52.00 KB)
Trent Taylor
Trent Taylor
StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
I had already traced the code. What I found on initial load was that the usernameBO was populated with values for Computername and Logged_In, but, return from the Add operation these 2 fields were null,


Are you initializing these fields when you handle your Add?  The issue you are running into is outside of the query, but rather elsewhere in your code.  I am willing to bet that the add is your culprit.  If you create a new user and inspect the CurrentDataTable, are there nulls in your table?  I bet that there are and that this is causing the issue.

Ian Hammond
Ian Hammond
StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)
Group: Forum Members
Posts: 57, Visits: 277
Hi Trent,

You are right, the fields are not initialised, although in my child form which is invoked when I click Add, on exit I do:

myUserNamesBO.NewRow();

myUserNamesBO.loggedIn =
false;

myUserNamesBO.computerName =
" ";

etc.

myUserNamesBO.Save();

I assumed that the fields were initialised. Can you say where is the best place to initialise the fields, am I missing something out?

Regards
Trent Taylor
Trent Taylor
StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)StrataFrame Developer (9.8K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
Well, it depends.  By default a BO will initialize all of the fields unless you tell it not to.  But the best place to initialize a field is in the SetDefaultValues event of the BO.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search