Link to home
Start Free TrialLog in
Avatar of triphen
triphen

asked on

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?
Avatar of triphen
triphen

ASKER

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
Avatar of Kimputer
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.
Avatar of triphen

ASKER

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....
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.
Avatar of triphen

ASKER

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?
Avatar of triphen

ASKER

I don't even care if PWDSalt column doesn't show, I just cant change the query from anything other than select * from.....
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

ASKER CERTIFIED SOLUTION
Avatar of triphen
triphen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of triphen

ASKER

This was the best way to handle my question.