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:
<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 :)