Aigini 0 Light Poster

The bold section of the code below is supposed to loop in the dataset and return the values from the second code snippet into the database.

The second snippet of code is for the data to be inserted into the database.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbType

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim OleConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data source= C:\CWMaster.mdb")
    
    Dim strFileName As String = "CwMaster"
    Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data source= C:\Documents and Settings\SL\Desktop\Project\CwDb.mdb"


    Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecute.Click
        Dim CwDbDataSet1 As System.Data.DataSet
   
        
        Dim MyConnection As System.Data.OleDb.OleDbConnection
        
        Try
            ''''''' Fetch Data from Excel

            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

            MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=C:\Documents and Settings\SL\Desktop\Project\CW 2008I-Update030708.xls;Extended Properties=Excel 8.0")

            MyConnection.Open()
            ' Select the data from Sheet5 of the workbook.
            MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [sheet5$]", MyConnection)
            CwDbDataSet1 = New System.Data.DataSet
            MyCommand.Fill(CwDbDataSet1)
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            DataGridView1.DataSource = CwDbDataSet1.Tables(0)
            MyConnection.Close()


           [B]Dim intRow As Integer

            MsgBox(CwDbDataSet1.Tables(0).Rows.Count)
            For intRow = 0 To CwDbDataSet1.Tables(0).Rows.Count - 1
                MsgBox(CwDbDataSet1.Tables(0).Rows(intRow).Item(0))
            Next[/B]

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Import Failed, correct Column name in the sheet!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'MyConnection.Close()

        End Try

     
        updateData()
    End Sub

The second snippet of code :

Public Sub updateData()
        Dim tranLocal As OleDbTransaction
        Dim CWNO As String
        Dim Name As String
        Dim ICNO As String
        Dim Nasion As String
        Dim Company As String
        Dim JoinDT As Date
        Dim ExpDT As Date
        ' Setup OleDbDataAdapter to use in Application

        ' Ad Hoc DB Query  to  insert
        Dim strInsert As String = "INSERT INTO " & strFileName
        strInsert &= " (CWNO, Name, ICNO, Nasion, Company,  JoinDT,  ExpDT )"
        strInsert &= " VALUES (@CWNO, @Name, @ICNO, @Nasion, @Company, @JoinDT, @ExpDT)"

        ' The DB Connection
        Dim con As New OleDbConnection(conStr)

        ' Setup Insert Command
        Dim strOLECommand As OleDbCommand = con.CreateCommand()
        strOLECommand.CommandType = CommandType.Text
        strOLECommand.CommandText = strInsert

        ' Add the parameters for the InsertCommand.

      [B]  strOLECommand.Parameters.Add("@CWNO", OleDbType.Numeric).Value = CWNO
        strOLECommand.Parameters.Add("@Name", OleDbType.VarChar).Value = Name
        strOLECommand.Parameters.Add("@ICNO", OleDbType.VarChar).Value = ICNO
        strOLECommand.Parameters.Add("@Nasion", OleDbType.VarChar).Value = Nasion
        strOLECommand.Parameters.Add("@Company", OleDbType.VarChar).Value = Company[/B]        
        
        strOLECommand.Parameters.Add("@JoinDT", OleDbType.Date).Value = JoinDT
        strOLECommand.Parameters.Add("@ExpDT", OleDbType.Date).Value = ExpDT
        'daTable.InsertCommand = strOLECommand

        strOLECommand.Transaction = tranLocal
        strOLECommand.ExecuteNonQuery()

    End Sub

But now the problem I face is for the second snippet of code, there is error for the bold section of the code, and it is related to the looping of the dataset. The error says that there is no value assigned to the variables yet. Please help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.