I'm doing a asp.net app with the code behind in .vb. Can I use C# in this as well?
I can not find any examples of using .vb only C#.
I want to upload the images to a folder in the app directory with their location in the db along with other values then display them with the values with the same ID.
Protected Sub btnUpload_Click
(ByVal sender As Object, ByVal e As EventArgs)
Dim strImageName As String = txtName.Text.ToString()
If FileUpload1.PostedFile IsNot Nothing AndAlso
FileUpload1.PostedFile.FileName <> "" Then
Dim imageSize As Byte() = New Byte
(FileUpload1.PostedFile.ContentLength - 1) {}
Dim uploadedImage__1 As HttpPostedFile =
FileUpload1.PostedFile
uploadedImage__1.InputStream.Read(imageSize, 0,
CInt(FileUpload1.PostedFile.ContentLength))
' Create SQL Connection
Dim con As New SqlConnection()
con.ConnectionString =
ConfigurationManager.ConnectionStrings
("ConnectionString").ConnectionString
' Create SQL Command
Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO Images
(ImageName,Image) VALUES (@ImageName,@Image)"
cmd.CommandType = CommandType.Text
cmd.Connection = con
Dim ImageName As New SqlParameter
("@ImageName", SqlDbType.VarChar, 50)
ImageName.Value = strImageName.ToString()
cmd.Parameters.Add(ImageName)
Dim UploadedImage__2 As New SqlParameter
("@Image", SqlDbType.Image, imageSize.Length)
UploadedImage__2.Value = imageSize
cmd.Parameters.Add(UploadedImage__2)
con.Open()
Dim result As Integer = cmd.ExecuteNonQuery()
con.Close()
If result > 0 Then
lblMessage.Text = "File Uploaded"
End If
GridView1.DataBind()
End If
End Sub
I wanted to beable to select other data from ddl and textboxes and browse for the image then when i hit the submit button to insert the ddl and tb data the image uploads with that data, but I am finding it hard to achieve this.
I am doing this with a local sql server out of vs2010, so anything with all the db connection code is pretty much useless.
I'm getting errors about a withevent on the submit button and a is not declared on the fileupload1 with this-
Private Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click
Dim strFileName As String
Dim strFilePath As String
Dim strFolder As String
strFolder = "~/Defendant_Images"
'Get the name of the file that is posted.
strFileName = FileUpload1.PostedFile.FileName
strFileName = Path.GetFileName(strFileName)
End Sub
The submit button and fileupload are inside a formview.....:|
I am trying to do this, maybe I can use an xml file to attach the images to a ID.
I am working on just getting the images to upload to the folder first with this-
Public Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click
Dim DefImageUpload As FileUpload
If DefImageUpload.HasFile Then
DefImageUpload.SaveAs("/Defendant_Images" & DefImageUpload.FileName)
End If
End Sub
and it is telling me that the DefImageUpLad is being used before it is assigned. Also the SubmitButton.Click is getting a WithEvents error???
JCP13
Member
37 Points
204 Posts
Image uploading?:
Jun 11, 2012 05:49 AM|LINK
I'm doing a asp.net app with the code behind in .vb. Can I use C# in this as well?
I can not find any examples of using .vb only C#.
I want to upload the images to a folder in the app directory with their location in the db along with other values then display them with the values with the same ID.
Primillo
Star
8731 Points
1681 Posts
Re: Image uploading?:
Jun 11, 2012 06:21 AM|LINK
Hi
Refer
http://www.aspnettutorials.com/tutorials/file/fileupload-vb.aspx
http://www.4guysfromrolla.com/articles/081303-1.aspx
Primillo
http://www.facebook.com/programandopuntonet
JCP13
Member
37 Points
204 Posts
Re: Image uploading?:
Jun 11, 2012 06:31 AM|LINK
First one is full of errors....
Second one isn't an option.
Primillo
Star
8731 Points
1681 Posts
Re: Image uploading?:
Jun 11, 2012 06:38 AM|LINK
On the #1 you create the folder Uploads in root?
Primillo
http://www.facebook.com/programandopuntonet
sanjayverma_...
Participant
1470 Points
347 Posts
Re: Image uploading?:
Jun 11, 2012 06:45 AM|LINK
hi ,
try this
Protected Sub btnUpload_Click (ByVal sender As Object, ByVal e As EventArgs) Dim strImageName As String = txtName.Text.ToString() If FileUpload1.PostedFile IsNot Nothing AndAlso FileUpload1.PostedFile.FileName <> "" Then Dim imageSize As Byte() = New Byte (FileUpload1.PostedFile.ContentLength - 1) {} Dim uploadedImage__1 As HttpPostedFile = FileUpload1.PostedFile uploadedImage__1.InputStream.Read(imageSize, 0, CInt(FileUpload1.PostedFile.ContentLength)) ' Create SQL Connection Dim con As New SqlConnection() con.ConnectionString = ConfigurationManager.ConnectionStrings ("ConnectionString").ConnectionString ' Create SQL Command Dim cmd As New SqlCommand() cmd.CommandText = "INSERT INTO Images (ImageName,Image) VALUES (@ImageName,@Image)" cmd.CommandType = CommandType.Text cmd.Connection = con Dim ImageName As New SqlParameter ("@ImageName", SqlDbType.VarChar, 50) ImageName.Value = strImageName.ToString() cmd.Parameters.Add(ImageName) Dim UploadedImage__2 As New SqlParameter ("@Image", SqlDbType.Image, imageSize.Length) UploadedImage__2.Value = imageSize cmd.Parameters.Add(UploadedImage__2) con.Open() Dim result As Integer = cmd.ExecuteNonQuery() con.Close() If result > 0 Then lblMessage.Text = "File Uploaded" End If GridView1.DataBind() End If End SubOR go with this link http://csharpdotnetfreak.blogspot.com/2009/07/fileupload-control-save-images-database.html
JCP13
Member
37 Points
204 Posts
Re: Image uploading?:
Jun 11, 2012 06:58 AM|LINK
I know, I changed it to the folder I have.
I wanted to beable to select other data from ddl and textboxes and browse for the image then when i hit the submit button to insert the ddl and tb data the image uploads with that data, but I am finding it hard to achieve this.
I am doing this with a local sql server out of vs2010, so anything with all the db connection code is pretty much useless.
RameshRajend...
Star
7983 Points
2099 Posts
Re: Image uploading?:
Jun 11, 2012 07:04 AM|LINK
Pls look this
http://www.aspnettutorials.com/tutorials/network/net-fileupload-aspnet2-vb.aspx
http://support.microsoft.com/kb/315832
thank u
JCP13
Member
37 Points
204 Posts
Re: Image uploading?:
Jun 11, 2012 07:16 AM|LINK
I'm getting errors about a withevent on the submit button and a is not declared on the fileupload1 with this-
Private Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click Dim strFileName As String Dim strFilePath As String Dim strFolder As String strFolder = "~/Defendant_Images" 'Get the name of the file that is posted. strFileName = FileUpload1.PostedFile.FileName strFileName = Path.GetFileName(strFileName) End SubThe submit button and fileupload are inside a formview.....:|
JCP13
Member
37 Points
204 Posts
Re: Image uploading?:
Jun 12, 2012 01:08 PM|LINK
I'm thinking that I will need to just insert the images in the DB along with all the other data it goes with.
The images with be small so spce shouldn't be a problem.
JCP13
Member
37 Points
204 Posts
Re: Image uploading?:
Jun 12, 2012 10:27 PM|LINK
I am trying to do this, maybe I can use an xml file to attach the images to a ID.
I am working on just getting the images to upload to the folder first with this-
Public Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click Dim DefImageUpload As FileUpload If DefImageUpload.HasFile Then DefImageUpload.SaveAs("/Defendant_Images" & DefImageUpload.FileName) End If End Suband it is telling me that the DefImageUpLad is being used before it is assigned. Also the SubmitButton.Click is getting a WithEvents error???