locked
Uploading images through the ObjectDataSource control RRS feed

  • Question

  • User-196219249 posted

    Question? Why is it when I use the SqlDataSource control that hard codes the SQL insert statement will allow me to add images in the database but when I use the ObjectDataSource control pointing to my vb class file I have issues inserting images? 

    My vb class code:

    Public Shared InsertRecord(ByVal Myimage As Byte, ByVal Fullname As String)
    
    
    Dim MyConnection As String = WebConfigurationManager.ConnectionStrings("MiscConnectionString").ConnectionString
    Dim conn As SqlConnection = New SqlConnection(MyConnection)
    Dim cmd As SqlCommand = New SqlCommand()
    cmd.Connection = con
    cmd.CommandText = "INSERT acctinfo (Image, FullName) VALUES (@Myimage, @Fullname)"
    cmd.CommandType = CommandType.Text
    
    cmd.Parameters.AddWithValue("@Image", Myimage)
    cmd.Parameters.AddWithValue("@Fullname", Fullname)
    
    Using conn
    Try
    conn.Open()
    cmd.ExecuteNonQuery()
    Catch ex As Exception
    Throw
    End Try
    conn.Close()
    End Using
    
    End Sub
    

    Client side code & Object Data Source:

    <div class="Box">
    <table>
    <tr>
      <td>
        <label>Full Name:</label>
      </td> 
      <td>
        <asp:TextBox ID="txtFull" runat="server" />
      </td>
    </tr>
    <tr>
      <td>
        <label>Upload Photo:</label>
      </td>
      <td>
        <asp:FileUpload ID="Upload" runat="server" />
      </td>
    </tr>
    </table>
    <div>
      <asp:Button ID="btnSave" runat="Server" Text="Save" Class="main_buttons" />
    </div>
    </div>
    
    
    <asp:ObjectDataSource
         ID="odsEmployeeSearch"
         runat="server"
         TypeName="GotData"
         SelectMethod="DisplayPeople"
         InsertMethod="InsertRecord">
    <SelectParameters>
       <asp:ControlParameters ControlID="txtSearch" Name="Search" PropertyName="Text" ConvertEmptyStringToNull="false" >
    </SelectParameters>
    <InsertParameters>
        <asp:ControlParameter ControlID="UpImage" Name="Myimage" PropertyName="FileBytes" />
        <asp:ControlParameter ControlID="txtFull" Name="Fullname" PropertyName="Text" /> 
    </InsertParameters>

    Code behind:

    Protected Sub Save_Details()
    
    odsEmployeeSearch.Insert()
    
    End Sub

    When I run the code I get the "Object of type 'System.Byte[]' cannot be converted to type 'System.Byte" error message.

    What can I do to make this work?

    Please don't send links to similar issues. I have read too many links that don't show a 3 tier setup.

    Thank you in an advance for those trying to help me,

    xyz789

    Monday, May 11, 2015 1:48 PM

Answers

  • User753101303 posted

    Hi,

    Public Shared InsertRecord(ByVal Myimage As Byte, ByVal Fullname As String)

    declares Myimage as being a single byte value rather than an array of bytes.

    What if you try :

    Public Shared InsertRecord(ByVal Myimage As Byte(), ByVal Fullname As String) instead?

    And this is exactly what the error message tells you that is that a byte array can't be converted to a single byte value...

    Edit:fixed HTML messed up. BTW I noticed you are rethrowing in the catch clause without doing anything else. I never understood the reason for doing that. My  guess is that this is just in case you would like to add some more code at at later time ??

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2015 2:43 PM

All replies

  • User753101303 posted

    Hi,

    Public Shared InsertRecord(ByVal Myimage As Byte, ByVal Fullname As String)

    declares Myimage as being a single byte value rather than an array of bytes.

    What if you try :

    Public Shared InsertRecord(ByVal Myimage As Byte(), ByVal Fullname As String) instead?

    And this is exactly what the error message tells you that is that a byte array can't be converted to a single byte value...

    Edit:fixed HTML messed up. BTW I noticed you are rethrowing in the catch clause without doing anything else. I never understood the reason for doing that. My  guess is that this is just in case you would like to add some more code at at later time ??

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 11, 2015 2:43 PM
  • User-196219249 posted

    Wow! Great eye!

    What was I thinking?

    Thank you so much for the help!

    Monday, May 11, 2015 3:03 PM