VB.net DataGridView Error

Hello Experts,

I am using Visual Studio VB.net 2010 with SyBAse SQL Anywhere v10. Here is my code....

     Try
                Dim str As String = "DSN=sqlbase;uid=dba;pwd=pass;"
                Dim con As New OdbcConnection(str)
                Dim com As String = "select * from employee"

                Dim Adpt As New OdbcDataAdapter(com, con)
                Dim ds As New DataSet()
                Adpt.Fill(ds)
                dgv_SQLResults.DataSource = ds.Tables(0)
                dgv_SQLResults.AutoResizeColumns()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error: Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                Exit Sub
            End Try

Open in new window


Produces this error....
---------------------------
DataGridView Default Error Dialog
---------------------------
The following exception occurred 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, DataGridViewDataErrorContexts context)



To replace this default dialog please handle the DataError event.
---------------------------
OK  
---------------------------



When specifying the columns, it works fine....

     Try
                Dim str As String = "DSN=sqlbase;uid=dba;pwd=pass;"
                Dim con As New OdbcConnection(str)
                Dim com As String = "select name, lastname, number, age from employee"

                Dim Adpt As New OdbcDataAdapter(com, con)
                Dim ds As New DataSet()
                Adpt.Fill(ds)
                dgv_SQLResults.DataSource = ds.Tables(0)
                dgv_SQLResults.AutoResizeColumns()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error: Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                Exit Sub
            End Try

Open in new window


I have a feeling the results are loading more columns than the DataGridView can display. Thoughts?
triphenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

triphenAuthor Commented:
Ok new information.....I dont think its loading "more columns than datagridview can display" rather it's a few specific columns that the datagridview cant display....see attached.
vb.net-error.png
0
KimputerCommented:
Probably the datagridview can't display the original bytes from database.
Either leave out the pwdsalt column, or add the columns one by one, so you have more control and have the pwdsalt column converted to strings. Since you import it automatically, VB.net thinks you want to import a picture or something, but the datagridview column wants a string.
0
triphenAuthor Commented:
Yes, looks like you are correct about VB trying to convert into image.

Leaving PWDSalt column out is not an option. Is there anyway I can tell VB to treat it like a string? I need VB to display "(Bytes)" like the screen shot from the other SQL editor.

I am hoping I can just change a property of the DataGridView to do this....
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

KimputerCommented:
Possibly this will help you out to convert bytes to strings: https://msdn.microsoft.com/en-us/library/ms172827.aspx
However, as I said, you can't use import everything in one go with the function you used. You have to read each column one by one, and only for the pwdsalt, do you read it and convert the data before passing it to the datagridview.
0
triphenAuthor Commented:
With the setup I have, I NEED TO run a "Select * from employee" there is no compromise here unfortunately.

Is there a way to handle this error and ignore it or something?
0
triphenAuthor Commented:
I don't even care if PWDSalt column doesn't show, I just cant change the query from anything other than select * from.....
0
KimputerCommented:
It's tedious to do it cell by cell, but I see no other way:

     Try
                Dim str As String = "DSN=sqlbase;uid=dba;pwd=pass;"
                Dim con As New OdbcConnection(str)
                Dim com As String = "select name, lastname, number, age from employee"

                Dim Adpt As New OdbcDataAdapter(com, con)
                Dim ds As New DataSet()
                Adpt.Fill(ds)
                dgv_SQLResults.DataSource = ds.Tables(0)
                dgv_SQLResults.AutoResizeColumns()
				
				
				        dgv_SQLResults.ColumnCount = ds.Tables(0).Columns.Count
        Dim counter = 0
        For Each col In ds.Tables(0).Columns
            If col.ToString = "pwdsalt" Then
                Dim pwdsalt_col = counter
            End If
            dgv_SQLResults.Columns(counter).Name = col.ToString
            counter = counter + 1
        Next

        'fill in rest
        Dim row
        For i = 0 To ds.Tables(0).Rows.Count - 1
            row = ds.Tables(0).Rows(i).ItemArray
            dgv_SQLResults.Rows.Add()
            counter = 0
            For Each item In row
                If (counter = pwdsalt_col) Then
                    dgv_SQLResults.Rows(i).Cells(counter).Value = UnicodeBytesToString(item)
                Else
                    dgv_SQLResults.Rows(i).Cells(counter).Value = item.ToString
                End If
                counter = counter + 1
            Next

        Next

		
		
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error: Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop)
                Exit Sub
            End Try

Open in new window


also add this somewhere else:

    Private Function UnicodeBytesToString(
    ByVal bytes() As Byte) As String

        Return System.Text.Encoding.Unicode.GetString(bytes)
    End Function

Open in new window

0
triphenAuthor Commented:
Adding this fixed the problem. Essentially it handles the error and does nothing.

Private Sub dgv_SQLResults_DataError(ByVal sender AsObject, ByVal e As DataGridViewDataErrorEventArgs) Handles dgv_SQLResults.DataError

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
triphenAuthor Commented:
This was the best way to handle my question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

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.