web 2.0

ASP.NET(vb.net) & Upload file into Database

ASP.NET(vb.net) & Upload file into Database - The in this tutorial, you’ll learn and example scripts how to Upload file and insert rows into database using by ASP.NET scripts.

ShotDev Focus:
- ASP.NET(vb.net) & Upload file into Database

Example

AspNetFileUploadToDatabase1.aspx

  1. <%@ Import Namespace="System.Data"%>  
  2. <%@ Import Namespace="System.Data.OleDb"%>  
  3. <%@ Page Language="VB" %>  
  4. <script runat="server">  
  5. Sub btnUpload_OnClick(sender As Object, e As EventArgs)  
  6.   
  7. Me.pnlUpload.Visible = False  
  8. Me.pnlSave.Visible = True  
  9.   
  10. If Me.fiUpload.HasFile = False Then  
  11. Me.lblText.Text = "Please select upload file!"  
  12. ELse  
  13. Me.fiUpload.SaveAs(Server.MapPath("Myfiles/"&fiUpload.FileName))  
  14. Me.lblText.Text = "<b>" &fiUpload.FileName & "</b> Uploaded.<br>"  
  15.   
  16. '*** Save to Database ***'  
  17. Dim objConn As OleDbConnection  
  18. Dim objCmd As OleDbCommand  
  19. Dim strConnString,strSQL As String  
  20.   
  21. strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"  
  22. objConn = New OleDbConnection(strConnString)  
  23. objConn.Open()  
  24.   
  25. strSQL = "INSERT INTO picture (PictureName) " & _  
  26. "VALUES ('" & fiUpload.FileName & "')"  
  27.   
  28. objCmd = New OleDbCommand()  
  29. With objCmd  
  30. .Connection = objConn  
  31. .CommandType = CommandType.Text  
  32. .CommandText = strSQL  
  33. End With  
  34. objCmd.ExecuteNonQuery()  
  35.   
  36. objCmd = Nothing  
  37. objConn.Close()  
  38. objConn = Nothing  
  39.   
  40. Me.hplLink1.NavigateUrl = "AspNetFileUploadToDatabase1.aspx"  
  41. Me.hplLink2.NavigateUrl = "AspNetFileUploadToDatabase2.aspx"  
  42. End IF  
  43. End Sub  
  44. </script>  
  45. <html>  
  46. <head>  
  47. <title>ShotDev.Com Tutorial</title>  
  48. </head>  
  49. <body>  
  50. <form id="form1" runat="server">  
  51. <asp:Panel id="pnlUpload" runat="server">  
  52. <asp:FileUpload id="fiUpload" runat="server"></asp:FileUpload>  
  53. <input id="btnUpload" type="button" OnServerClick="btnUpload_OnClick"  value="Upload" runat="server" />  
  54. </asp:Panel>  
  55. <asp:Panel id="pnlSave" Visible="false" runat="server">  
  56. <asp:Label id="lblText" runat="server"></asp:Label><br />  
  57. <asp:HyperLink id="hplLink1" runat="server">Upload again</asp:HyperLink>  
  58. ,  
  59. <asp:HyperLink id="hplLink2" runat="server">View files</asp:HyperLink>  
  60. </asp:Panel>  
  61. </form>  
  62. </body>  
  63. </html>  

AspNetFileUploadToDatabase2.aspx

  1. <%@ Import Namespace="System.Data"%>  
  2. <%@ Import Namespace="System.Data.OleDb"%>  
  3. <%@ Page Language="VB" %>  
  4. <script runat="server">  
  5. Dim objConn As OleDbConnection  
  6. Dim objCmd As OleDbCommand  
  7.   
  8. Sub Page_Load(sender As Object, e As EventArgs)  
  9. Dim strConnString As String  
  10. strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";"  
  11. objConn = New OleDbConnection(strConnString)  
  12. objConn.Open()  
  13.   
  14. BindData()  
  15. End Sub  
  16.   
  17. Sub BindData()  
  18. Dim strSQL As String  
  19. strSQL = "SELECT * FROM picture"  
  20.   
  21. Dim dtReader As OleDbDataReader  
  22. objCmd = New OleDbCommand(strSQL, objConn)  
  23. dtReader = objCmd.ExecuteReader()  
  24.   
  25. '*** BindData to Repeater ***'  
  26. myRepeater.DataSource = dtReader  
  27. myRepeater.DataBind()  
  28.   
  29. dtReader.Close()  
  30. dtReader = Nothing  
  31.   
  32. End Sub  
  33.   
  34. Sub Page_UnLoad()  
  35. objConn.Close()  
  36. objConn = Nothing  
  37. End Sub  
  38. </script>  
  39. <html>  
  40. <head>  
  41. <title>ShotDev.Com Tutorial</title>  
  42. </head>  
  43. <body>  
  44. <form id="form1" runat="server">  
  45. <asp:Repeater id="myRepeater" runat="server">  
  46. <HeaderTemplate>  
  47. <table border="1">  
  48. <tr>  
  49. <th>PictureID</th>  
  50. <th>PictureName</th>  
  51. </tr>  
  52. </HeaderTemplate>  
  53. <ItemTemplate>  
  54. <tr>  
  55. <td align="center"><%#Container.DataItem("PictureID") %></td>  
  56. <td><a href="Myfiles/<%#Container.DataItem("PictureName") %>" target="blank">  
  57. <%#Container.DataItem("PictureName") %></a>  
  58. </td>  
  59. </tr>  
  60. </ItemTemplate>  
  61. </asp:Repeater>  
  62. </form>  
  63. </body>  
  64. </html>  

Screenshot

ASP.NET(vb.net) & Upload file into Database

ASP.NET(vb.net) & Upload file into Database
.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (No Ratings Yet)
Loading ... Loading ...

One Response to “ASP.NET(vb.net) & Upload file into Database”

  1. 1materialism…

Leave a Reply

You must be logged in to post a comment.