addykha
addykha

Reputation: 61

Operator '&' is not defined for types 'Object' and '1-dimensional array of byte'

Good day/night! I'm making an .Net application using Mysql as my database and currently stuck in a problem on my query. I'm allowing the user to upload images, however I'm getting this error on the 'ImageData' variable which is a Byte datatype

Operator '&' is not defined for types 'Object' and '1-dimensional array of byte'

on my console as I click the button for saving it to the database.

Here's my code on the click event

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    'DECLARE THE STRING VARIABLE THAT YOU'RE GOING TO USE.
    Dim sqlQUERY As String
    'FORMAT IMAGE TO BE ABLE TO SAVE TO DATABASE
    Dim fs As FileStream
    Dim br As BinaryReader

    Try
        If txtbox_image.Text.Length > 0 Then
            Dim FileName As String = txtbox_image.Text
            Dim ImageData() As Byte

            fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
            br = New BinaryReader(fs)
            ImageData = br.ReadBytes(CType(fs.Length, Integer))
            br.Close()
            fs.Close()
            'STORE YOUR QUERY TO A VARIABLE THAT YOU HAVE DECLARED.
            sqlQUERY = "INSERT INTO vids.vehicles " & _
                       "VALUES (" & _
                       " NULL, " & _
                       " '" & txtbox_plateno.Text.ToString() & "', " & _
                       " '" & txtbox_model.Text.ToString() & "', " & _
                       " '" & txtbox_manufacturer.Text.ToString() & "', " & _
                       " '" & Format(dtp_year.Value, "yyyy").ToString() & "', " & _
                       " '" & Format(dtp_dateacq.Value, "yyyy-MM-dd").ToString() & "', " & _
                       " (SELECT vehicle_type_id from vehicle_types where type_name='" & DirectCast(cb_vehicletype.SelectedItem, DataRowView).Item("type_name") & "'), " & _
                       " '" & Format(dtp_reg.Value, "yyyy-MM-dd").ToString() & "', " & _
                       " '" & Format(dtp_regexpire.Value, "yyyy-MM-dd").ToString() & "', " & _
                       ImageData & ", now(), now());"
            'CALL THE METHOD THAT YOU HAVE CREATED AND PUT YOUR SQLQUERY IN THE PARAMETERS' LIST
            SAVING(sqlQUERY)
            RELOAD(Form1.dgv_vehicles)
            Me.Close()
        Else
            MsgBox("Incomplete data!", MsgBoxStyle.Critical, "")
        End If

    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try
    
End Sub

The 'SAVING' subroutine is placed in a Module so that it would be reusable.

'CREATE A SUB PROCEDURE IN SAVING THE DATA WITH THE PARAMETER TYPE OF STRING
Public Sub SAVING(ByVal sqlQuery As String)
    Try
        'OPENING THE CONNECTION
        strcon.Open()
        'INITIALIZE YOUR COMMANDS
        'IT HOLDS THE DATA TO BE EXECUTED
        With cmd
            'PASS ON THE VALUE OF STRCON TO THE MYSQL COMMANND WHICH IS THE CONNECTION
            .Connection = strcon
            'THE FUNCTION OF THIS IS TO RETURN THE TEXT REPRESENTED BY A COMMAND OBJECT
            .CommandText = sqlQuery
        End With
        'IT EXECUTES THE DATA THAT HAS TO BE SAVE IN THE DATABASE.
        res = cmd.ExecuteNonQuery

        'DATA WILL NOT BE SAVED IF IT EXECUTES LESS THAN 0
        'BUT IF IT EXECUTES GREATER THAN 0, THE DATA WILL BE SAVED.
        If res = 0 Then
            MsgBox("Error in saving!", MsgBoxStyle.Exclamation)
        Else
            MsgBox("The data has been saved.")
        End If

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    'CLOSING THE CONNECTION
    strcon.Close()
End Sub

I tried using DirectCast(ImageData, Byte) but it returns an

Value of type '1-dimensional array of Byte' cannot be converted to 'Byte'.

I'm currently new to VB.Net so I don't have extensive knowledge about Datatypes and its conversions

Thanks!

Upvotes: 0

Views: 3186

Answers (2)

bob
bob

Reputation: 1

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim colours(9) As String
    Dim totals(9) As Integer

End Sub

Private Sub populatelists(ByRef colours() As String, ByRef totals() As Integer)

    For row = 0 To 9
        colours = InputBox("please enter the colour of car you saw?")
        totals = InputBox("please enter how many times you saw this colour")
    Next

End Sub

Private Sub findmostpopular(ByVal colours() As String, ByVal totals() As Integer, ByRef highest As Integer, ByRef mostpopcolour As String)

    mostpopcolour = colours(0)
    highest = totals(0)

    For row = 1 To 9

        If totals > highest Then
            highest = totals(row)
            colours = colours(row)
        End If
    Next

End Sub

Private Sub findleastpopular(ByVal colours() As String, ByVal totals() As Integer, ByRef lowest As Integer, ByRef leastpopcolour As String)

    leastpopcolour = colours(0)
    lowest = totals(0)

    For row = 1 To 9

        If totals < lowest Then
            lowest = totals(row)
            colours = colours(row)
        End If
    Next


End Sub

Private Sub displayresults(ByVal highest As Integer, ByVal mostpopcolour As String, ByVal lowest As Integer, ByVal leastpopcolour As String)

    output.Items.Add("the highest amount of cars seen were " & highest & " " & mostpopcolour & " cars")
    output.Items.Add("       ")
    output.Items.Add("the lowest amount of cars seen were " & lowest & " " & leastpopcolour & " cars")

End Sub

End Class

Upvotes: 0

Pradeep Kumar
Pradeep Kumar

Reputation: 6979

I won't get into the the debate of best practices/using parameterized queries etc. You need to read on that stuff yourself.

In the code you posted, I would avoid string concatenation the way you do and use String.Format instead, which results in cleaner code and also avoids problems similar to what you are facing.

See the difference for yourself.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    'DECLARE THE STRING VARIABLE THAT YOU'RE GOING TO USE.
    Dim sqlQUERY As String
    'FORMAT IMAGE TO BE ABLE TO SAVE TO DATABASE
    Dim fs As FileStream
    Dim br As BinaryReader

    Try
        If txtbox_image.Text.Length > 0 Then
            Dim FileName As String = txtbox_image.Text
            Dim ImageData() As Byte

            fs = New FileStream(FileName, FileMode.Open, FileAccess.Read)
            br = New BinaryReader(fs)
            ImageData = br.ReadBytes(CType(fs.Length, Integer))
            br.Close()
            fs.Close()
            'STORE YOUR QUERY TO A VARIABLE THAT YOU HAVE DECLARED.
            sqlQUERY = String.Format("INSERT INTO vids.vehicles VALUES ( NULL, '{0}', '{1}', '{2}', '{3:yyyy}', '{4:yyyy-MM-dd}', (SELECT vehicle_type_id from vehicle_types where type_name='{5}'), '{6:yyyy-MM-dd}', '{7:yyyy-MM-dd}', {8}, now(), now());", _
                                     txtbox_plateno.Text, txtbox_model.Text, txtbox_manufacturer, dtp_year.Value, dtp_dateacq.Value, DirectCast(cb_vehicletype.SelectedItem, DataRowView).Item("type_name"), dtp_reg.Value, dtp_regexpire.Value, ImageData)
            'CALL THE METHOD THAT YOU HAVE CREATED AND PUT YOUR SQLQUERY IN THE PARAMETERS' LIST
            SAVING(sqlQUERY)
            RELOAD(Form1.dgv_vehicles)
            Me.Close()
        Else
            MsgBox("Incomplete data!", MsgBoxStyle.Critical, "")
        End If

    Catch ex As Exception
        MsgBox(ex.ToString())
    End Try
End Sub

Upvotes: 1

Related Questions