We help IT Professionals succeed at work.

Error When Displaying Query In Dataview

1,383 Views
Last Modified: 2013-11-26
Hello,

I have a datagridview which the user populates with whatever query they want to type which queries the database specified by their connection string.

It's working great with only one problem that I can tell.  If the user writes a query that contains an image field I get the following error message:

The following exception occured in the DataGridView:  

System.ArgumentException: Parameter is not valid.
  at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
 at System.Drawing.ImageConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
 at System.Windows.Forms.Formatter.FormatObjectInternal(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetConverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue)
 at System.Windows.Forms.Formatter.FormatObject(Object value, Type targetType, TypeConverter sourceConverter, TypeConverter targetconverter, String formatString, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
 at System.Windows.Forms.DataGridViewCell.GetFormattedValue(Object value, Int32 rowIndex, DataGridViewCellStyle& cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridVewDataErrorContexts context)

To replace this default dialog please handle the DataError event.

How can I prevent this error from occurring, or prevent the user from querying an Image field?

Thanks,
-Torrwin
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for putting me on the right track at least.  Here's how I did it:

With dsTemp.Tables(0)
     For i As Integer = 0 To dsTemp.Tables(0).Columns.Count - 1
          If InStr(.Columns(i).DataType.ToString, GetType(Byte).ToString) > 0 Then
               dsTemp.Tables(0).Columns(i).ColumnMapping = MappingType.Hidden
          End If
     Next
End With

DataGridView1.DataSource = dsTemp.Tables(0)

Where dsTemp is the dataset with your query results.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.