Q:

Error when writing to excel worksheet using vb.net

I am getting an error message when I am attempting to write to an excel worksheet using VB.net 2005  

Operation must use an updateable query  

I have gone thru the S/O search results
https://stackoverflow.com/search?q=Operation+must+use+an+updateable+query,
but no response resolve my issue.  
My code is:  
Dim strXLScn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & xlsFilePath.Text & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""

        Dim XLConn As New OleDb.OleDbConnection(strXLSconn)
        Dim XLcmd As New System.Data.OleDb.OleDbCommand
        Dim sSQLWriteToExcel As String

        strExcel = "INSERT INTO [Sheet1$A1:A1] Values ('" & sCity & "')"

        Dim oleCMD As New OleDb.OleDbCommand(strExcel, XLConn)
        Dim oleDA As New OleDb.OleDbDataAdapter(oleCMD)

        XLConn.Open()
        XLcmd.Connection = XLConn

        XLcmd.CommandText = strExcel
        XLcmd.ExecuteNonQuery()
        XLConn.Close()

Any ideas?

A:

First things first, make sure you have Option Explicit ON, this will help with variables that you are trying to use that you never declared.  Second, remove the IMEX=1 from the end of your connection string.  Third, make sure your excel document has a header row.  This code worked for me:
     Dim strXLScn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\Temp\test.xls"";Extended Properties=""Excel 8.0;HDR=Yes"""

    Dim XLConn As New OleDb.OleDbConnection(strXLScn)
    Dim XLcmd As New System.Data.OleDb.OleDbCommand
    Dim strExcel As String
    Dim sCity As String = "Sydney"
    strExcel = "INSERT INTO [Sheet1$] Values ('" & sCity & "')"

    Dim oleCMD As New OleDb.OleDbCommand(strExcel, XLConn)
    Dim oleDA As New OleDb.OleDbDataAdapter(oleCMD)

    XLConn.Open()
    XLcmd.Connection = XLConn

    XLcmd.CommandText = strExcel
    XLcmd.ExecuteNonQuery()
    XLConn.Close()

