Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all..

I am working in asp.net with vb.net to create a group in the site
but I faced a problem with this code, but I feel that my code is correct. The problem is, when I run the code, the picture does not upload, and return there was an error. Please help me.


in group.aspx i wrote this:

XML
<asp:FileUpload ID="imgUpload" runat="server" />
    <br />
      <asp:Label ID="lblResult" runat="server" ForeColor="#0066FF"></asp:Label>
<br />
   <asp:Image ID="Image1" style="width:200px" Runat="server" />




and in code-behand

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Data.OleDb
Imports System
Imports System.Configuration
Imports System.Web
Imports System.IO

Partial Class creatgroup
    Inherits System.Web.UI.Page

    Private Shared connectionString As String = _
  WebConfigurationManager.ConnectionStrings("Pubs").ConnectionString
    Private myConnection As New SqlConnection(connectionString)
   
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try

            Dim img As FileUpload = CType(imgUpload, FileUpload)
            Dim imgByte As Byte() = Nothing
            If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
                'To create a PostedFile
                Dim File As HttpPostedFile = imgUpload.PostedFile
                'Create byte Array with file len
                imgByte = New Byte(File.ContentLength - 1) {}
                'force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength)
            End If
            ' Insert the Group info and image into db


            myConnection.Open()
            Dim sql As String = "INSERT INTO Group(GroupName,Pic,Description,category,Privacy,News) VALUES(@enm, @eimg, @Description, @category, @Privacy, @News) @@IDENTITY"
            Dim cmd As SqlCommand = New SqlCommand(sql, myConnection)
            cmd.Parameters.AddWithValue("@enm", GroupName.Text)
            cmd.Parameters.AddWithValue("@eimg", imgByte)
            cmd.Parameters.AddWithValue("@Description", Description.Text)
            cmd.Parameters.AddWithValue("@category", Category.SelectedValue)

            If Privacy.Checked Then
                cmd.Parameters.AddWithValue("@Privacy", "True")
            Else
                cmd.Parameters.AddWithValue("@Privacy", "False")
            End If
            cmd.Parameters.AddWithValue("@News", News.Text)
            Dim id As String = cmd.ExecuteScalar.ToString
            lblResult.Text = String.Format("Group ID is {0}", id)
        Catch
            lblResult.Text = "There was an error"
        Finally
            myConnection.Close()
        End Try
    End Sub

End Class



Regards :)
Posted
Updated 25-Apr-10 7:56am
v3

Why do you have curly braces at the end of this line :

imgByte = New Byte(File.ContentLength - 1) {}
 
Share this answer
 
meshosho wrote:
Dim img As FileUpload = CType(imgUpload, FileUpload)


Why do you need to cast this ? Why is it not the right type already ? I assume this is the control on the page?

Have you stepped through this code to see what happens ? Is imgByte Nothing when it hits the DB code ? Why do you have the DB code run even if there is no image ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900