I'm using Visual Web Designer 2010, VB. I'm trying to create a routing whereby I can read an Excel file, manipulate the data (by adding additional columns and information) and output it back to Excel. I found the routine (a starting point) below on the web but cannot get it to work. The first thing I did was to create an empty table in the SQL database named ExtendedBOM. The problem is now with Button3, the "For Each dr AS DataRow In ds1.Tables(0).Rows" , I get the message: "Cannot find Table 0." and I'm at a complete loss as to what the problem is. Does anybody have any suggestions what I can try next? The spreadsheet I'm trying to read in has 5 columns (Item, Quantity, Part_Number, Reference_Designators, Description) and the first line contains the header text. Thanks!
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Partial Class Excel
Inherits System.Web.UI.Page
Dim ds1 As New DataSet
Dim ds2 As New DataSet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim _filename As String = "C:\new.xls"
Dim _conn As String
_conn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & _filename & ";" & "Extended Properties=Excel 8.0;"
Dim _connection As OleDbConnection = New OleDbConnection(_conn)
Dim da As OleDbDataAdapter = New OleDbDataAdapter()
Dim _command As OleDbCommand = New OleDbCommand()
_command.Connection = _connection
_command.CommandText = "SELECT * FROM [Sheet1$]"
da.SelectCommand = _command
Try
da.Fill(ds1, "sheet1")
Response.Write("The import is complete!")
Me.DataGridView1.DataSource = ds1
Me.DataGridView1.DataMember = "sheet1"
Catch e1 As Exception
Response.Write("Import Failed, correct Column name in the sheet!")
End Try
End Sub
Dim da As SqlDataAdapter
Dim conn As SqlConnection
Dim cb As SqlCommandBuilder
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conCString").ConnectionString)
Dim sel As String = "SELECT * FROM ExtendedBOM"
da = New SqlDataAdapter(sel, conn)
cb = New SqlCommandBuilder(da)
da.MissingSchemaAction = MissingSchemaAction.AddWithKey
da.Fill(ds2, "ExtendedBOM")
Me.DataGridView1.DataSource = ds2
Me.DataGridView1.DataMember = "ExtendedBOM"
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
For Each dr As DataRow In ds1.Tables(0).Rows 'ERROR THIS LINE
Dim expression As String
expression = "myId =" + CType(dr.Item(0), Integer).ToString
Dim drs() As DataRow = ds2.Tables(0).Select(expression)
If (drs.Length = 1) Then
For i As Integer = 1 To ds2.Tables(0).Columns.Count - 1
drs(0).Item(i) = dr.Item(i)
Next
Else
Dim drnew As DataRow = ds2.Tables(0).NewRow
For i As Integer = 0 To ds2.Tables(0).Columns.Count - 1
drnew.Item(i) = dr.Item(i)
Next
ds2.Tables(0).Rows.Add(drnew)
End If
Next
Me.DataGridView1.DataSource = ds2
Me.DataGridView1.DataMember = "ExtendedBOM"
da.Update(ds2.Tables(0))
End Sub
End Class
The asp portion is below:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Excel.aspx.vb" Debug="true" Inherits="Excel" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title></title></head><body><form id="form1" runat="server"><div><asp:Button ID="Button1" runat="server" Text="Button" /><br /><asp:Button ID="Button2" runat="server" Text="Button" /><br /><asp:Button ID="Button3" runat="server" Text="Button" /><br /><br /></div><asp:GridView ID="DataGridView1" runat="server"></asp:GridView></form></body></html>