Click here to Skip to main content
15,884,975 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i write a program that i need to update in the *dbf file from data in the *xls file using specific id. now i have 2 problem that i need a help. 1, the error is display MsgBox("Can not open connection ! ") after i placed this:
VB
Dim dBaseCommand As New System.Data.OleDb.OleDbCommand("UPDATE paytran.DBF SET OT1 = 2  WHERE EMPNO = 102", dBaseConnection)

   Dim dBaseDataReader As System.Data.OleDb.OleDbDataReader = dBaseCommand.ExecuteReader(CommandBehavior.SequentialAccess)

here is my code:
VB
Dim strpath As String = File_Name
Dim dbfpath As String = File_dbf

Dim ConnectionString As String

Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim range As Excel.Range
Dim rCnt As Integer

Dim Empno As Object

Dim empno1 As String

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\file\;Extended Properties=dBase IV"
Dim dBaseConnection As New System.Data.OleDb.OleDbConnection(ConnectionString)

Try
    dBaseConnection.Open()

    xlApp = CreateObject("Excel.Application")
    xlWorkBook = xlApp.Workbooks.Open(strpath)
    xlWorkSheet = xlWorkBook.Worksheets("sheet1")

    range = xlWorkSheet.UsedRange
    For rCnt = 1 To range.Rows.Count 'count row

        Empno = CType(range.Cells(rCnt, 1), Excel.Range)
        empno1 = Empno.value

    Next

    Dim dBaseCommand As New System.Data.OleDb.OleDbCommand("UPDATE paytran.DBF SET OT1 = 2  WHERE EMPNO = 102", dBaseConnection)
    Dim dBaseDataReader As System.Data.OleDb.OleDbDataReader = dBaseCommand.ExecuteReader(CommandBehavior.SequentialAccess)

    xlWorkBook.Close()
    xlApp.Quit()

    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)

    dBaseConnection.Close()

Catch ex As Exception
    MsgBox("Can not open connection ! ")
End Try

Private Sub releaseObject(ByVal obj As Object)
Try
    System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
    obj = Nothing
Catch ex As Exception
    obj = Nothing
Finally
    GC.Collect()
End Try
End Sub

2, i think my query update at the wrong place and wrong syntax. this is what i want

VB
update paytran.dbf set OT1 = [column in xls file H1] where EMPNO = [column in xls file A1]


i hope codeproject member can help me..
Posted

1 solution

You need something like this:
VB
Dim ConnectionString As String = String.Empty, sCommand As String = String.Empty
Dim rCnt As Integer = 0, Empno As Integer = 0, Otvalue As Integer = 0, retVal As Integer = 0
Dim xlApp As Excel.Application = Nothing, xlWorkBook As Excel.Workbook = Nothing, xlWorkSheet As Excel.Worksheet = Nothing, xlRange As Excel.Range = Nothing
'or
'Dim xlApp As Object = Nothing, xlWorkBook As Object = Nothing, xlWorkSheet As Object = Nothing, xlRange As Object = Nothing
Dim dBaseConnection As OleDb.OleDbConnection = Nothing, dBaseCommand As OleDb.OleDbCommand = Nothing

Try
    ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & IO.Path.GetDirectoryName(File_dbf) & ";Extended Properties=dBase IV"
    dBaseConnection = New OleDb.OleDbConnection(ConnectionString)
    dBaseConnection.Open()

    xlApp  = New Excel.Application
    'or - if xlApp is declared as Object
    'xlApp = CreateObject("Excel.Application")
    xlWorkBook = xlApp.Workbooks.Open(File_Name)
    xlWorkSheet = xlWorkBook.Worksheets("sheet1")
    xlRange = xlWorkSheet.UsedRange

    For rCnt = 1 To xlRange.Rows.Count 'count row
        Empno = xlRange.Cells(rCnt, 1).Value2
        Otvalue = xlRange.Cells(rCnt, 3).Value2
        '("UPDATE paytran.DBF SET OT1 = 2  WHERE EMPNO = 102", dBaseConnection)
        sCommand = "UPDATE paytran.DBF SET OT1 = " & Otvalue.ToString & "  WHERE EMPNO = " & Empno.ToString
        dBaseCommand = New OleDb.OleDbCommand(sCommand, dBaseConnection)
        'how many records updated?
        retVal = dBaseCommand.ExecuteNonQuery()
    Next

    xlWorkBook.Close()
    xlApp.Quit()

    dBaseConnection.Close()

Catch ex As System.InvalidCastException
    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")

Catch ex As OleDb.OleDbException
    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")

Catch ex As System.NullReferenceException
    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")

Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error...")

Finally
    'if dbconnection is opened, close it
    'check dBaseConnection.State
    releaseObject(xlApp)
    releaseObject(xlWorkBook)
    releaseObject(xlWorkSheet)
End Try
 
Share this answer
 
Comments
xana7900 20-Mar-13 21:24pm    
i think im getting close to solution...
can i know if i need to place FOR..NEXT loop to update every single row data from excel to dbf,where can i place it...?
i hope u can teach me...thanks for your help..
Maciej Los 21-Mar-13 2:55am    
xana7900, please, have a look at code. Inside for... loop i create command to update database.
xana7900 20-Mar-13 21:54pm    
i get this error 'External table is not in the expected format'..is it related to Dim ConnectionString As String = String.Empty..
Maciej Los 21-Mar-13 2:53am    
No, it's not "related to" Dim ConnectionString AS String=String.Empty, because, ConnectionString was initiated in Try ... Catch ... Finally code block.
xana7900 21-Mar-13 2:57am    
why have an error 'External table is not in the expected format'...i try to change the provider..but new error was appear....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900