VB.NET - OleDb write to Excel file works in XP but gives "must use updateable query" error in Windows 7

I'm writing a VB.NET application in VS2008 that uses Excel as a back-end to store some simple data. The entire application code is attached below. On Windows XP, this works exactly as intended, but on Windows 7, I get the following error:

System.Data.OleDb.OleDbException: Operation must use an updateable query.

Can anyone tell me how to update my code to make it Windows 7 compatible?
Imports Excel = Microsoft.Office.Interop.Excel


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
        Dim x As Integer = 0
        If Not (txtFirstName.Text = "" Or txtLastName.Text = "" Or txtCompany.Text = "" Or txtEmail.Text = "") Then

            Dim FirstName, LastName, Email, Company, Title, Phone As String

            FirstName = txtFirstName.Text
            LastName = txtLastName.Text
            Email = txtEmail.Text
            Company = txtCompany.Text
            Title = txtTitle.Text
            Phone = txtPhone.Text

            Try
                Dim MyConnection As System.Data.OleDb.OleDbConnection
                Dim MyCommand As New System.Data.OleDb.OleDbCommand
                MyConnection = New System.Data.OleDb.OleDbConnection _
                ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Data.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES;""")

                MyConnection.Open()
                MyCommand.Connection = MyConnection
                MyCommand.CommandText = "INSERT INTO [Sheet1$] ([First Name],[Last Name],[Email],[Company],[Title],[Phone]) values ('" & FirstName & "','" & LastName & "','" & Email & "','" & Company & "','" & Title & "','" & Phone & "')"
                MyCommand.ExecuteNonQuery()
                MyConnection.Close()

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

            txtFirstName.Text = ""
            txtLastName.Text = ""
            txtEmail.Text = ""
            txtCompany.Text = ""
            txtTitle.Text = ""
            txtPhone.Text = ""

        Else
            x = MsgBox("Please fill out all required fields.", vbOKOnly, "Required fields empty")
        End If

    End Sub

    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

   
End Class

Open in new window

LCMSdevAsked:
Who is Participating?
 
CodeCruiserCommented:
Your Excel file is in C root. Windows 7 does not allow programs to access C root, windows folder, program files folder and many other places. Try moving it to another drive or within a folder such as desktop.
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Follow the CodeCruiser instructions, or run your program with administrative privileges to make it work (I prefer the CodeCruiser advice).
0
 
LCMSdevAuthor Commented:
Yup, that did it. Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.