943,150 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 5145
  • ASP.NET RSS
Mar 6th, 2010
0

upload file into sql server using asp.net and vb.net

Expand Post »
i found this code to upload a file into a sql database using the asp.net fileupload object (or vb)
i found it here

http://forums.asp.net/p/1480079/3451771.aspx

i think it works, but i am a learning noob and have a question...where does my dbase info go in the code?

ASP.NET Syntax (Toggle Plain Text)
  1. 1. If FleUpload.HasFile Then
  2. 2. Dim fileName As String = Server.HtmlEncode(FleUpload.FileName)
  3. 3. Dim extension As String = System.IO.Path.GetExtension(fileName)
  4. 4. If (extension.ToUpper = ".JPG") Or (extension.ToUpper = ".GIF") Then
  5. 5.
  6. 6. '**** Resize image section ****
  7. 7. Dim image_file As System.Drawing.Image = System.Drawing.Image.FromStream(FleUpload.PostedFile.InputStream)
  8. 8. Dim image_height As Integer = image_file.Height
  9. 9. Dim image_width As Integer = image_file.Width
  10. 10. Dim max_height As Integer = 120
  11. 11. Dim max_width As Integer = 160
  12. 12.
  13. 13.
  14. 14. image_height = (image_height * max_width) / image_width
  15. 15. image_width = max_width
  16. 16.
  17. 17. If image_height > max_height Then
  18. 18. image_width = (image_width * max_height) / image_height
  19. 19. image_height = max_height
  20. 20. Else
  21. 21. End If
  22. 22.
  23. 23.
  24. 24. Dim bitmap_file As New Bitmap(image_file, image_width, image_height)
  25. 25. Dim stream As New System.IO.MemoryStream
  26. 26.
  27. 27. bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
  28. 28. stream.Position = 0
  29. 29.
  30. 30. Dim data(stream.Length) As Byte
  31. 31. stream.Read(data, 0, stream.Length)
  32. 32. '**** End resize image section ****
  33. 33.
  34. 34.
  35. 35. Dim myConn As New SqlConnection(ConfigurationManager.ConnectionStrings("cs").ConnectionString)
  36. 36. Dim mycmd As New SqlCommand("se_equipmentimages_insert", myConn)
  37. 37. mycmd.CommandType = CommandType.StoredProcedure
  38. 38.
  39. 39. mycmd.Parameters.AddWithValue("@equipment_id", id)
  40. 40. mycmd.Parameters.AddWithValue("@image_file", data)
  41. 41.
  42. 42. Try
  43. 43. myConn.Open()
  44. 44. mycmd.ExecuteNonQuery()
  45. 45. Catch ex As Exception
  46. 46. Finally
  47. 47. myConn.Close()
  48. 48. End Try
  49. 49.
  50. 50. Else
  51. 51. lblError.Text = "Please only upload .jpg or .gif files"
  52. 52. lblError.Visible = True
  53. 53. End If
  54. 54. Else
  55. 55. lblError.Text = "No file selected"
  56. 56. lblError.Visible = True
  57. 57. End If

to wit...

dbase name: mydBase
table name: dBaseTable
column names :fileOne, fileTwo, fileThree

where in the code above does this go?
also, how does this code work for multiple fileuploads at once?

thanks

james
Reputation Points: 10
Solved Threads: 0
Newbie Poster
front_&_center is offline Offline
1 posts
since Mar 2010
Mar 7th, 2010
0
Re: upload file into sql server using asp.net and vb.net
>where does my dbase info go in the code?

Line #35.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,525 posts
since Oct 2008
Mar 7th, 2010
0
Re: upload file into sql server using asp.net and vb.net
The following line reads the database connection information from web.config file.
ASP.NET Syntax (Toggle Plain Text)
  1. 35. Dim myConn As New SqlConnection(ConfigurationManager.ConnectionStrings("cs").ConnectionString)

The sql server connection details can be stored in web.config something like as below.
ASP.NET Syntax (Toggle Plain Text)
  1. <appSettings>
  2. <add key="cs" value="Server=YourServerName;Database=Northwind;User Id=testuser;Password=somepassword;" />
  3. </appSettings>
You can also specify the connection details in the code itself

ASP.NET Syntax (Toggle Plain Text)
  1. Dim myConn As New SqlConnection("Server=YourServerName;Database=Northwind;User ID=testuser;Password=somepassword;")
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009
Feb 8th, 2011
0
Re: upload file into sql server using asp.net and vb.net
pls mark thread as solved if solved
Reputation Points: 43
Solved Threads: 67
Veteran Poster
Netcode is online now Online
1,013 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: upload file from client to server without openFile dialog
Next Thread in ASP.NET Forum Timeline: How to show tab vertically





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC