Error When Displaying Query In Dataview

Posted on 2007-10-08
Last Modified: 2013-11-26

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?

Question by:Torrwin
    LVL 22

    Accepted Solution

    Take a look at this, it deals with storing and retrieving images

    Or you could check the column datatypes like this

    LVL 13

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now