WPF/LinqToSql/DataGrid

B

Bill McCormick

Hello,

Selecting certain rows of data from my WPF datagrid (bound to a
LinqToSQL datasource) are generating exceptions that I'm not sure how to
handle.

It's a FormatException with message: String must be exactly one
character long, but I can't tell what it is about these records that is
causing the problem.

The only info I know how to get is the stack trace (below) and it's not
helping me.


Thanks,


Bill McCormick
--
MSDN Subscriber

System.FormatException was unhandled
Message="String must be exactly one character long."
Source="mscorlib"
StackTrace:
at System.Convert.ToChar(String value, IFormatProvider provider)
at System.String.System.IConvertible.ToChar(IFormatProvider
provider)
at System.Convert.ChangeType(Object value, Type conversionType,
IFormatProvider provider)
at System.Data.Linq.DBConvert.ChangeType(Object value, Type type)
at Read_Dsp(ObjectMaterializer`1 )
at
System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Data.Linq.EntitySet`1.Load()
at System.Data.Linq.EntitySet`1.get_Count()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at System.Data.Linq.EntitySet`1.GetNewBindingList()
at
System.Data.Linq.EntitySet`1.System.ComponentModel.IListSource.GetList()
at MS.Internal.Data.ViewManager.GetViewRecord(Object collection,
CollectionViewSource cvs, Type collectionViewType, Boolean createView)
at MS.Internal.Data.DataBindEngine.GetViewRecord(Object
collection, CollectionViewSource key, Type collectionViewType, Boolean
createView)
at
System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object
source, Boolean createView)
at
System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object
source, DependencyObject d)
at
System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value)
at
System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject
d, DependencyPropertyChangedEventArgs e)
at
System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs
e)
at
System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs
e)
at
System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs
args)
at
System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex
entryIndex, DependencyProperty dp, PropertyMetadata metadata,
EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean
coerceWithDeferredReference, OperationType operationType)
at
System.Windows.StyleHelper.ApplyTemplatedParentValue(DependencyObject
container, FrameworkObject child, Int32 childIndex, FrugalStructList`1&
childRecordFromChildIndex, DependencyProperty dp,
FrameworkElementFactory templateRoot)
at
System.Windows.StyleHelper.InvalidatePropertiesOnTemplateNode(DependencyObject
container, FrameworkObject child, Int32 childIndex, FrugalStructList`1&
childRecordFromChildIndex, Boolean isDetach, FrameworkElementFactory
templateRoot)
at
System.Windows.StyleHelper.LoadOptimizedTemplateContent(DependencyObject
container, ParserContext parserContext, OptimizedTemplateContent
optimizedTemplateContent, FrameworkTemplate frameworkTemplate,
IComponentConnector componentConnector, IStyleConnector styleConnector,
List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
at System.Windows.FrameworkTemplate.LoadContent(DependencyObject
container, List`1 affectedChildren, UncommonField`1
templatedNonFeChildrenField)
at
System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1
dataField, DependencyObject container, FrameworkElementFactory
templateRoot, Int32 lastChildIndex, HybridDictionary
childIndexFromChildID, FrameworkTemplate frameworkTemplate)
at
System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1
templateDataField, FrameworkElement container)
at System.Windows.FrameworkElement.ApplyTemplate()
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean
forceInfinityV)
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32
cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean
forceInfinityV)
at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.ContextLayoutManager.UpdateLayout()
at
System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
at
System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object
resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object
resizedCompositionTarget)
at
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object
source, Delegate callback, Object args, Boolean isSingleParameter,
Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate
callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at
System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object
state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at
System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode
code, CleanupCode backoutCode, Object userData)
at
System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,
Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr
wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at
System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate
callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object
source, Delegate callback, Object args, Boolean isSingleParameter,
Delegate catchHandler)
at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate
callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at
System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority
priority, TimeSpan timeout, Delegate method, Object args, Boolean
isSingleParameter)
at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority
priority, Delegate method, Object arg)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg,
IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at
System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame
frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at ScAggScale.App.Main() in C:\ace-co-devel\Aceco32\VS2008\Agg
Load Out\ScAggScale\obj\Debug\App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(Assembly assembly,
String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile,
Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
 
B

Bill McCormick

Peter said:
Hint: don't put important information in your post below your signature.
Many news readers simply hide the signature, others format it with less
emphasis than the rest of the post, and either way the important
information is difficult or impossible to access.

Right. Thanks.
As for the question, it's not clear to me what the question actually is.
The exception seems pretty clear to me: you're trying to convert a string,
but the conversion requires that the string be only one character long.

1st sentence: "... generating exceptions that I'm not sure how to handle."
In this particular case, it appears that the conversion is to the
System.Char type, which makes perfect sense. What would the conversion do
if you tried to convert something longer than one character to System.Char?

The fix is to stop trying to squeeze multiple characters into a single
character. Whether this means you need to fix the data, so that it's only
one character long, or you need to fix the destination, so that it has the
type System.String instead of System.Char, I can't say. You didn't post a
concise-but-complete code sample that reliably demonstrates the problem.
But I think it's likely the solution will be along the lines of one or the
other of those approaches.

I'd gladly post a complete and concise code sample if I knew what that
would be; I wouldn't want to just post the whole solution. But if you
could suggest something specific, I'd gladly post it.

So I have 2 questions:

1. How do I handle these exceptions? Where in code should it be done? In
the code behind the XAML for the UserControl that the DataGrid is on? Or
in the App.xaml.cs? Maybe the Main.xaml.cs? Or maybe the designer.cs
module. I understand only partially what the problem and how to fix it
for lack of information.

2. As you say, the "fix is to stop trying to squeeze multiple characters
into a single character." I saw that, but without more information from
the IDE, I can't tell which field I'm trying to *squeeze* data into. So
where do I look to get the information I need?


Thanks,


Bill McCormick
 
B

Bill McCormick

Peter said:
Hint: don't put important information in your post below your signature.
Many news readers simply hide the signature, others format it with less
emphasis than the rest of the post, and either way the important
information is difficult or impossible to access.

Right. Thanks.
As for the question, it's not clear to me what the question actually is.
The exception seems pretty clear to me: you're trying to convert a string,
but the conversion requires that the string be only one character long.

1st sentence: "... generating exceptions that I'm not sure how to handle."
In this particular case, it appears that the conversion is to the
System.Char type, which makes perfect sense. What would the conversion do
if you tried to convert something longer than one character to System.Char?

The fix is to stop trying to squeeze multiple characters into a single
character. Whether this means you need to fix the data, so that it's only
one character long, or you need to fix the destination, so that it has the
type System.String instead of System.Char, I can't say. You didn't post a
concise-but-complete code sample that reliably demonstrates the problem.
But I think it's likely the solution will be along the lines of one or the
other of those approaches.

I'd gladly post a complete and concise code sample if I knew what that
would be; I wouldn't want to just post the whole solution. But if you
could suggest something specific, I'd gladly post it.

So I have 2 questions:

1. How do I handle these exceptions? Where in code should it be done? In
the code behind the XAML for the UserControl that the DataGrid is on? Or
in the App.xaml.cs? Maybe the Main.xaml.cs? Or maybe the designer.cs
module. I understand only partially what the problem and how to fix it
for lack of information.

2. As you say, the "fix is to stop trying to squeeze multiple characters
into a single character." I saw that, but without more information from
the IDE, I can't tell which field I'm trying to *squeeze* data into. So
where do I look to get the information I need?


Thanks,


Bill McCormick
 
P

Pavel Minaev

Selecting certain rows of data from my WPF datagrid (bound to a
LinqToSQL datasource) are generating exceptions that I'm not sure how to
handle.

It's a FormatException with message: String must be exactly one
character long, but I can't tell what it is about these records that is
causing the problem.

The only info I know how to get is the stack trace (below) and it's not
helping me.
[snip]

Looking at the stack trace, I have a strong feeling that WPF is not
even relevant here. What happens when you use the exact same LINQ
query and just enumerate the result completely, without databinding it
to anything?

I think what happens is that you have a divergence between your LINQ
to SQL-generated C# classes, and the database schema. Your class
probably has a "char" field, and the corresponding field of the table
contains strings that are longer than a single char.
 
B

Bill McCormick

Peter Duniho wrote:
[snip]
I don't think that handling the exception is relevant. The exception is
informing you of a bug in your code. Fix the bug, the exception goes
away. It's not an exception that _needs_ handling, or even one that
should be..

That's not a good answer. All exceptions should be able to be handled.

Bill
 
B

Bill McCormick

Pavel said:
Selecting certain rows of data from my WPF datagrid (bound to a
LinqToSQL datasource) are generating exceptions that I'm not sure how to
handle.

It's a FormatException with message: String must be exactly one
character long, but I can't tell what it is about these records that is
causing the problem.

The only info I know how to get is the stack trace (below) and it's not
helping me.
[snip]

Looking at the stack trace, I have a strong feeling that WPF is not
even relevant here. What happens when you use the exact same LINQ
query and just enumerate the result completely, without databinding it
to anything?

Yes, that's what I was thinking as well. Unfortunately, I can't look
under the hood enough to see what query is being generated at the point
it fails.
I think what happens is that you have a divergence between your LINQ
to SQL-generated C# classes, and the database schema. Your class
probably has a "char" field, and the corresponding field of the table
contains strings that are longer than a single char.

There's only one char field in the class and it's corresponding SQL
field is a VarChar(1).

I'm getting nowhere. I'm really hoping to get some MS support on this. I
hate to use a support incident for this.


Thanks,

Bill
 
L

Linda Liu[MSFT]

Hi Bill,

Pete and Pavel have given valuable replies, which are also what I think
about your issue.

Please have a try starting the application by pressing F11 to find out the
line of code that throws the exception.

If it doesn't help, it would be better if you could reproduce the problem
in a simple project. Thus I can look further into this issue and may find
the real reason that causes the problem. My email address is
(e-mail address removed).

Thank you for your understanding and I look forward to your reply.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu[MSFT]

Hi Bill,

Pete and Pavel have given valuable replies, which are also what I think
about your issue.

Please have a try starting the application by pressing F11 to find out the
line of code that throws the exception.

If it doesn't help, it would be better if you could reproduce the problem
in a simple project. Thus I can look further into this issue and may find
the real reason that causes the problem. My email address is
(e-mail address removed).

Thank you for your understanding and I look forward to your reply.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Bill McCormick

Linda said:
Hi Bill,

Pete and Pavel have given valuable replies, which are also what I think
about your issue.

Please have a try starting the application by pressing F11 to find out the
line of code that throws the exception.

If it doesn't help, it would be better if you could reproduce the problem
in a simple project. Thus I can look further into this issue and may find
the real reason that causes the problem. My email address is
(e-mail address removed).

Thank you for your understanding and I look forward to your reply.

Thanks for your reply Linda.

Pete and Pavel have indeed given valuable replies, however I think they
may not completely understand.

I think being able to try-and-catch exceptions is central to developing
solid code and being able to catch the exception I have is completely
relevant. And it's difficult to post a *proper code example* when the
IDE has auto-generated all (most of) the code. Should I post LinqToSql
designer.cs code? I don't think so.

I do have a char field in the class, but the corresponding DB field is a
VarChar(1) so I don't see a problem there.

Anyway, at this point, that's all I have is a simple program. I don't
think stepping into the application is an option since the problem
doesn't occur until I try to select a record that has the problem.

Please stay with me on this, I really need your help!!


Thanks,


Bill
 
B

Bill McCormick

Linda said:
Hi Bill,

Pete and Pavel have given valuable replies, which are also what I think
about your issue.

Please have a try starting the application by pressing F11 to find out the
line of code that throws the exception.

If it doesn't help, it would be better if you could reproduce the problem
in a simple project. Thus I can look further into this issue and may find
the real reason that causes the problem. My email address is
(e-mail address removed).

Thank you for your understanding and I look forward to your reply.

Thanks for your reply Linda.

Pete and Pavel have indeed given valuable replies, however I think they
may not completely understand.

I think being able to try-and-catch exceptions is central to developing
solid code and being able to catch the exception I have is completely
relevant. And it's difficult to post a *proper code example* when the
IDE has auto-generated all (most of) the code. Should I post LinqToSql
designer.cs code? I don't think so.

I do have a char field in the class, but the corresponding DB field is a
VarChar(1) so I don't see a problem there.

Anyway, at this point, that's all I have is a simple program. I don't
think stepping into the application is an option since the problem
doesn't occur until I try to select a record that has the problem.

Please stay with me on this, I really need your help!!


Thanks,


Bill
 
L

Linda Liu[MSFT]

Hi Bill,

Thank you for your reply!

Well, since your application is a simple program, please send the project
as well as the Database to my email box and tell me how to reproduce the
problem.

I look forward to your reply.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu[MSFT]

Hi Bill,

Thank you for your reply!

Well, since your application is a simple program, please send the project
as well as the Database to my email box and tell me how to reproduce the
problem.

I look forward to your reply.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu[MSFT]

Hi Bill,

I haven't heard of you for more than two days.

How about the problem now? If you need our further assistance, please feel
free to let us know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu[MSFT]

Hi Bill,

I haven't heard of you for more than two days.

How about the problem now? If you need our further assistance, please feel
free to let us know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
 
B

Bill McCormick

OK. It's a small project with a large test database. It will take a
little time for me to purge some of the data.

Thanks,


Bill
 
B

Bill McCormick

OK. It's a small project with a large test database. It will take a
little time for me to purge some of the data.

Thanks,


Bill
 
B

Bill McCormick

Linda said:
Hi Bill,

Thank you for your reply!

Well, since your application is a simple program, please send the project
as well as the Database to my email box and tell me how to reproduce the
problem.

I sent you the project. Let me know if you got it ok.


Thanks,

Bill
 
B

Bill McCormick

Linda said:
Hi Bill,

Thank you for your reply!

Well, since your application is a simple program, please send the project
as well as the Database to my email box and tell me how to reproduce the
problem.

I sent you the project. Let me know if you got it ok.


Thanks,

Bill
 
L

Linda Liu[MSFT]

Hi Bill,

Thank you for your reply!

But I haven't received your sample until now. Please ensure my email box is
(e-mail address removed).


Sincerely,
Linda Liu
Microsoft Online Community Support
 
L

Linda Liu[MSFT]

Hi Bill,

Thank you for your reply!

But I haven't received your sample until now. Please ensure my email box is
(e-mail address removed).


Sincerely,
Linda Liu
Microsoft Online Community Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top