Hi All,

im in the process of creating a mini auction in vb.net, so far i have deisgned the pages and now im beginning to code, but im stuck on the Fileupload of the image, this is the first time i have used a file upload so im looking for guidance is possible.

My SQL Column where i want to save the image is set up as a data type image but when i run the application i get this error "Operand type clash: nvarchar is incompatible with image"

My Code behind the BtnAuction Item

If ItemPicture.HasFile Then
            Try
                Dim FileName As String = Server.HtmlEncode(ItemPicture.FileName)
                Dim Extension As String = System.IO.Path.GetExtension(FileName)
                If (Extension.ToUpper = ".JPG") Or (Extension.ToUpper = ".GIF") Or (Extension.ToUpper = ".PNG") Then
                    Dim image_file As System.Drawing.Image = System.Drawing.Image.FromStream(ItemPicture.PostedFile.InputStream)
                    Dim image_height As Integer = image_file.Height
                    Dim image_width As Integer = image_file.Width
                    Dim max_height As Integer = 12011
                    Dim max_width As Integer = 160

                    image_height = (image_height * max_width) / image_width
                    image_width = max_width

                    If image_height > max_height Then
                        image_width = (image_width * max_height) / image_height
                    Else
                    End If

                    Dim BitMap_File As New Bitmap(image_file, image_width, image_height)
                    Dim Stream As New System.IO.MemoryStream

                    BitMap_File.Save(Stream, System.Drawing.Imaging.ImageFormat.Jpeg)
                    Stream.Position = 0
                    Dim data(Stream.Length) As Byte
                    Stream.Read(data, 0, Stream.Length)
                    Picture = BitMap_File
                End If
            Catch ex As Exception
            End Try
        End If
        GlobalHash.Add("@spItemTitle", txtAuctionTitle.Text)
        GlobalHash.Add("@spItemCondition", ddlCondition.SelectedValue)
        GlobalHash.Add("@spStartingPrice", txtStartingPrice.Text)
        GlobalHash.Add("@spItemStatus", status)
        GlobalHash.Add("@spItemDescription", txtItemDescription.Text)
        GlobalHash.Add("@spItemPicture", Picture)
        GlobalHash.Add("@spItemOwner", UserID)
        NewItem.InsertItem(GlobalHash)
    End Sub

This is my function where i try and add the image aswell as the relevant information

Public Function InsertItem(ByVal ItemDetails As Hashtable) As Boolean

        Dim SpParameter As DictionaryEntry
        Dim cmd As New SqlCommand

        cmd.CommandText = "NewItemToAuction"
        cmd.CommandType = CommandType.StoredProcedure
        For Each SpParameter In ItemDetails
            cmd.Parameters.Add(New SqlParameter(SpParameter.Key, SpParameter.Value.ToString))
        Next
        Dim m_Connection As New SQLAccess
        If m_Connection.EstablishConnection = True Then
            cmd.Connection = m_Connection.SQLSVRConnection
            cmd.ExecuteNonQuery()
            Return True
        Else
            Return False
        End If
    End Function

If someone can help me i would highly appreciate it, as i have been banging my head on this for the past two days

Scott.Atkinson,

I read your problem and this is not difficult.
I am giving you two codes choose which ever you like.

Fist in Design part :

<asp:FileUpload id="FileUploadControl" runat="server" />
    <asp:Button runat="server" id="Button1" text="Upload" onclick="Button1_Click" />
    <br />
    <asp:Label runat="server" id="msgshow" text="Upload status: " />

Then in the code part

namespace : using system.IO;

if(FileUploadControl.HasFile)
    {
        try
        {
            string filename = Path.GetFileName(FileUploadControl.FileName);
            FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
            msgshow.Text = "Upload status: File uploaded!";
        }
        catch(Exception ex)
        {
            msgshow.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }

and the other code is :

protected void UploadButton_Click(object sender, EventArgs e)
{
    if(FileUploadControl.HasFile)
    {
        try
        {
            if(FileUploadControl.PostedFile.ContentType == "image/jpeg")
            {
                if(FileUploadControl.PostedFile.ContentLength < 102400)
                {
                    string filename = Path.GetFileName(FileUploadControl.FileName);
                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                    StatusLabel.Text = "Upload status: File uploaded!";
                }
                else
                    StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
            }
            else
                StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
        }
        catch(Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

If you Still find difficulty then you can ask again. Hope this help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.