The entire code behind:
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Partial Class UploadProducts
Inherits System.Web.UI.Page
Protected Sub ButtonUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonUpload.Click
PanelUpload.Visible = True
PanelView.Visible = False
PanelImport.Visible = False
End Sub
Protected Function ExcelConnection() As OleDbCommand
' Connect to the Excel Spreadsheet
Dim xConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("~/ExcelImport.xls") & ";" & _
"Extended Properties=Excel 8.0;"
' create your excel connection object using the connection string
Dim objXConn As New OleDbConnection(xConnStr)
objXConn.Open()
' use a SQL Select command to retrieve the data from the Excel Spreadsheet
' the "table name" is the name of the worksheet within the spreadsheet
' in this case, the worksheet name is "Members" and is coded as: [Members$]
Dim objCommand As New OleDbCommand("SELECT * FROM [Products$]", objXConn)
Return objCommand
'the products$ is the worksheet name in excel not the workbook
End Function
Protected Sub ButtonView_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles ButtonView.Click
PanelUpload.Visible = False
PanelView.Visible = True
PanelImport.Visible = False
' Create a new Adapter
Dim objDataAdapter As New OleDbDataAdapter()
' retrieve the Select command for the Spreadsheet
objDataAdapter.SelectCommand = ExcelConnection()
' Create a DataSet
Dim objDataSet As New DataSet()
' Populate the DataSet with the spreadsheet worksheet data
objDataAdapter.Fill(objDataSet)
' Bind the data to the GridView
GridViewExcel.DataSource = objDataSet.Tables(0).DefaultView
GridViewExcel.DataBind()
End Sub
Protected Sub ButtonImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonImport.Click
PanelUpload.Visible = False
PanelView.Visible = False
PanelView.Visible = True
labelImport.Text = "" ' resert to blank
' retrieve the Select Command for the worksheet data
Dim objCommand As New OleDbCommand()
objCommand = ExcelConnection()
' create a DataReader
Dim reader As OleDbDataReader
reader = objCommand.ExecuteReader()
Dim counter As Integer = 0 ' used for testing your import in smaller increments
While reader.Read()
counter = counter + 1 ' counter to exit early for testing...
' set default values for loop
Dim D2ProductID As Integer = Convert.ToString(reader("ProductID"))
Dim D2ProductName As String = Convert.ToString(reader("ProductName"))
Dim D2ProductShortDescrip As String = Convert.ToString(reader("ProductShortDescrip"))
Dim D2ProductLongDescrip As String = Convert.ToString(reader("ProductLongDescrip"))
Dim D2ProductPrice As System.Nullable(Of Decimal) = Convert.ToString(reader("ProductPrice"))
Dim D2ProductStockLevel As System.Nullable(Of Integer) = Convert.ToString(reader("ProductStockLevel"))
Dim D2ProductActive As Boolean = Convert.ToString(reader("ProductActive"))
Dim D2ProductShipCode As System.Nullable(Of Integer) = Convert.ToString(reader("ProductShipCode"))
Dim D2ProductSpecial As Boolean = Convert.ToString(reader("ProductSpecial"))
Dim D2ProductSortOrder As Integer = Convert.ToString(reader("ProductSortOrder"))
Dim D2ProductCatID As System.Nullable(Of Integer) = Convert.ToString(reader("ProductCatID"))
Dim D2ProductWeight As Integer = Convert.ToString(reader("ProductWeight"))
Dim D2ProductImage1 As String = Convert.ToString(reader("ProductImage1"))
Dim D2ProductImage2 As String = Convert.ToString(reader("ProductImage2"))
Dim D2ProductImage3 As String = Convert.ToString(reader("ProductImage3"))
Dim D2ProductImage4 As String = Convert.ToString(reader("ProductImage4"))
Dim D2TaxExempt As Boolean = Convert.ToString(reader("TaxExempt"))
Dim D2EstimatedShipping As Decimal = Convert.ToString(reader("EstimatedShipping"))
Dim D2AllowReviews As Boolean = Convert.ToString(reader("AllowReviews"))
Dim D2IsDownload As Boolean = Convert.ToString(reader("IsDownload"))
Dim D2DownloadPath As String = Convert.ToString(reader("DownloadPath"))
Dim D2DownloadSize As System.Nullable(Of Integer) = Convert.ToString(reader("DownloadSize"))
Dim D2DownloadCap As Integer = Convert.ToString(reader("DownloadCap"))
Dim D2DownloadGUID As String = Convert.ToString(reader("DownloadGUID"))
Dim D2SoftwareVersion As String = Convert.ToString(reader("SoftwareVersion"))
Dim D2FileType As String = Convert.ToString(reader("FileType"))
Dim D2FileName As String = Convert.ToString(reader("FileName"))
Dim D2DownloadExpiry As Integer = Convert.ToString(reader("DownloadExpiry"))
Dim D2DateAdded As Date = Convert.ToString(reader("DateAdded"))
Dim D2ProductCode As String = Convert.ToString(reader("ProductCode"))
Dim D2Manufacturer As String = Convert.ToString(reader("Manufacturer"))
Dim D2CatID2 As System.Nullable(Of Integer) = Convert.ToString(reader("CatID2"))
Dim D2PreviousPrice As System.Nullable(Of Decimal) = Convert.ToString(reader("PreviousPrice"))
Dim D2ProductArea As String = Convert.ToString(reader("ProductArea"))
End While
reader.Close()
End Sub
Protected Sub ButtonUploadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonUploadFile.Click
If FileUploadExcel.HasFile Then
Try
' alter path for your project
FileUploadExcel.SaveAs(Server.MapPath("~/ExcelImport.xls"))
LabelUpload.Text = "Upload File Name: " & _
FileUploadExcel.PostedFile.FileName & "<br>" & _
"Type: " & _
FileUploadExcel.PostedFile.ContentType & _
" File Size: " & _
FileUploadExcel.PostedFile.ContentLength & " kb<br>"
Catch ex As Exception
LabelUpload.Text = "Error: " & ex.Message.ToString
End Try
Else
LabelUpload.Text = "Please select a file to upload."
End If
End Sub
Protected Function ImportIntoProducts(ByVal D2ProductArea As String, ByVal D2ProductName As String, _
ByVal D2ProductShortDescrip As String, ByVal D2ProductLongDescrip As String, ByVal D2ProductPrice As String, ByVal D2ProductStockLevel As String, ByVal D2ProductActive As String, ByVal D2ProductShipCode As String, ByVal D2ProductSpecial As String, ByVal D2ProductSortOrder As String, ByVal D2ProductCatID As String, ByVal D2ProductWeight As String, ByVal D2ProductImage1 As String, ByVal D2ProductImage2 As String, ByVal D2ProductImage3 As String, ByVal D2ProductImage4 As String, ByVal D2TaxExempt As String, ByVal D2EstimatedShipping As String, ByVal D2AllowReviews As String, ByVal D2IsDownload As String, ByVal D2DownloadPath As String, ByVal D2DownloadSize As String, ByVal D2DownloadCap As String, ByVal D2DownloadGUID As String, ByVal D2SoftwareVersion As String, ByVal D2DownloadBinary As String, ByVal D2FileType As String, ByVal D2FileName As String, ByVal D2DownloadExpiry As String, ByVal D2DateAdded As String, ByVal D2ProductCode As String, _
ByVal D2Manufacturer As String, ByVal D2CatID2 As String, ByVal D2PreviousPrice As String, ByVal D2ProductID As Integer) As Integer
D2ProductID = Left(D2ProductID, 100)
D2ProductName = Left(D2ProductName, 100)
D2ProductShortDescrip = Left(D2ProductShortDescrip, 100)
D2ProductLongDescrip = Left(D2ProductLongDescrip, 200)
D2ProductPrice = Left(D2ProductPrice, 100)
D2ProductStockLevel = Left(D2ProductStockLevel, 100)
D2ProductActive = Left(D2ProductActive, 100)
D2ProductShipCode = Left(D2ProductShipCode, 100)
D2ProductSpecial = Left(D2ProductSpecial, 100)
D2ProductSortOrder = Left(D2ProductSortOrder, 100)
D2ProductCatID = Left(D2ProductCatID, 100)
D2ProductWeight = Left(D2ProductWeight, 100)
D2ProductImage1 = Left(D2ProductImage1, 100)
D2ProductImage2 = Left(D2ProductImage2, 100)
D2ProductImage3 = Left(D2ProductImage3, 100)
D2ProductImage4 = Left(D2ProductImage4, 100)
D2TaxExempt = Left(D2TaxExempt, 100)
D2EstimatedShipping = Left(D2EstimatedShipping, 100)
D2AllowReviews = Left(D2AllowReviews, 100)
D2IsDownload = Left(D2IsDownload, 100)
D2DownloadPath = Left(D2DownloadPath, 100)
D2DownloadSize = Left(D2DownloadSize, 100)
D2DownloadCap = Left(D2DownloadCap, 100)
D2DownloadGUID = Left(D2DownloadGUID, 100)
D2SoftwareVersion = Left(D2SoftwareVersion, 100)
D2DownloadBinary = Left(D2DownloadBinary, 100)
D2FileType = Left(D2FileType, 100)
D2FileName = Left(D2FileName, 100)
D2DownloadExpiry = Left(D2DownloadExpiry, 100)
D2DateAdded = Left(D2DateAdded, 100)
D2ProductCode = Left(D2ProductCode, 100)
D2Manufacturer = Left(D2Manufacturer, 100)
D2CatID2 = Left(D2CatID2, 100)
D2PreviousPrice = Left(D2PreviousPrice, 100)
D2ProductArea = Left(D2ProductArea, 100)
Dim ProductID As Integer = 0
Try
Dim D2Adapter As New ProductImpDataSetTableAdapters.SLProductsTableAdap ter
Dim SLProductsDataTable As ProductImpDataSet.SLProductsDataTable = Nothing
SLProductsDataTable = D2Adapter.GetProductsData
If Not SLProductsDataTable Is Nothing Then
If SLProductsDataTable.Rows.Count > 0 Then
If Not SLProductsDataTable(0).D2ProductID Then
D2ProductID = SLProductsDataTable(0).D2ProductID
LabelImport.Text &= "Product Found, Not Imported:"&"ID:"& D2ProductID & ".<br>"
End If
End If
End If
If D2ProductID = 0 Then
D2ProductID = _
Convert.ToInt64(D2Adapter.InsertProductsQuery(D2Pr oductName As String, D2ProductShortDescrip As String, D2ProductLongDescrip As String, D2ProductPrice As System.Nullable(Of Decimal), D2ProductStockLevel As System.Nullable(Of Integer), D2ProductActive As Boolean, D2ProductShipCode As System.Nullable(Of Integer), D2ProductSpecial As Boolean, D2ProductSortOrder As Integer, D2ProductCatID As System.Nullable(Of Integer), D2ProductWeight As Integer, D2ProductImage1 As String, D2ProductImage2 As String, D2ProductImage3 As String, D2ProductImage4 As String, D2TaxExempt As Boolean, D2EstimatedShipping As Decimal, D2AllowReviews As Boolean, D2IsDownload As Boolean, D2DownloadPath As String, D2DownloadSize As System.Nullable(Of Integer), D2DownloadCap As Integer, D2DownloadGUID As String, D2SoftwareVersion As String, D2DownloadBinary() As Byte, D2FileType As String, D2FileName As String, D2DownloadExpiry As Integer, D2DateAdded As Date, D2ProductCode As String, D2Manufacturer As String, D2CatID2 As System.Nullable(Of Integer), D2PreviousPrice As System.Nullable(Of Decimal), D2ProductArea As String, D2ProductID As Integer) As Integer
LabelImport.Text &= "Products Imported " & " ID:" & D2ProductID & ". <br>"
End If
Return D2ProductID
Catch ex As Exception
LabelImport.Text &= "" & ex.ToString & "<br/>"
Return 0
End Try
End Function
End Class
The ASPX
<%@ Page Language="
VB" MasterPageFile="~/SLMaster.master" AutoEventWireup="false" CodeFile="UploadProducts.aspx.
vb" Inherits="UploadProducts" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style="width: 517px; height: 117px">
<tr>
<td style="width: 111px">
<asp:Button ID="ButtonUpload" runat="server"
text="Upload Excel Spreadsheet" />
<asp:Panel ID= "PanelUpload" runat="server" Visible="false">
Please select an Excel file to import:<br />
<asp:FileUpload ID="FileUploadExcel" runat="server" />
<br /><br />
<asp:Button ID="ButtonUploadFile" runat="server" Text="Upload File" />
<br /><br />
<asp:Label ID="LabelUpload" runat="server" Text=""></asp:Label>
</asp:Panel>
</td>
<td style="width: 118px">
<asp:Button ID="ButtonView" runat="server"
text="View Excel Data" />
<asp:Panel ID="PanelView" runat="server">
<asp:GridView ID="GridViewExcel" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#DCDCDC" />
</asp:GridView>
</asp:Panel>
</td>
<td style="width: 3px">
<asp:Button ID="ButtonImport" runat="server"
text="Import Excel Data" />
<asp:Panel ID="PanelImport" runat="server">
</asp:Panel>
<asp:Label ID="LabelImport" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
</asp:Content>