Error When Displaying Query In Dataview


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?

LVL 13
Who is Participating?
Take a look at this, it deals with storing and retrieving images

Or you could check the column datatypes like this

TorrwinAuthor 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
End With

DataGridView1.DataSource = dsTemp.Tables(0)

Where dsTemp is the dataset with your query results.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.