简体   繁体   中英

Connection.Close() error ASP.NET

It is a simple data insert script I've found which is writing into access.db Here is the problem I'm having from the codes below:

Error: Line 16: Connection.Open()

If you please let me know what exactly should I simply edit in the codes with, that would be great.

Many Thanks

    <%@ Page Language="VB" Debug="true"%>
<%@ Import Namespace="System.Data.Oledb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Page.IsPostBack = False Then
            Label1.Visible = False
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Connection As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=C:\inetpub\wwwroot\Thinkdebt\customers.mdb")
        Connection.Open()
        Dim Command As New OleDbCommand("INSERT INTO tblCustomers(FirstName," & _
        "LastName)VALUES(@FirstName,@LastName)", Connection)
        Command.Parameters.Add(New OleDbParameter("@FirstName", TextBox1.Text))
        Command.Parameters.Add(New OleDbParameter("@LastName", TextBox2.Text))
        Command.ExecuteNonQuery()

        Connection.Close()
        Label1.Text = "Record inserted."
        Label1.Visible = True

        TextBox1.Text = ""
        TextBox2.Text = ""
    End Sub

 </script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id=Head1 runat="server">
<title>ASP.NET Form to Database Sample</title>
</head>
<body>
<div>
<h1>ASP.NET Form to Database</h1>
<form id="form1" runat="server">
First name:<asp:TextBox ID="TextBox1" runat="server" Text=""></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" runat="server" ErrorMessage="Please enter a first name"></asp:RequiredFieldValidator><br /><br />
Last name:<asp:TextBox ID="TextBox2" runat="server" Text=""></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TextBox2" runat="server" ErrorMessage="Please enter a last name"></asp:RequiredFieldValidator><br /><br />

<asp:Button ID=Button1 runat="server" Text="Insert Record" OnClick="Button1_Click" />
<br />
<asp:Label ID=Label1 runat="server" Text=""></asp:Label>
</form>
</div>
</body>
</html>

jet.Oledb.4.0 doesnt work for 64Bit machine.

You can use Microsoft.ACE.OLEDB.12.0 for 64 bit system. You can check is it 32 bit or 64 bit system. If 32 bit then use .JET.OLEDB else use ACE.OLEDB.

you are using the Jet.OLEDB.4.0 driver, which gives that error when run on 64 bit system, it is better to install the new driver Microsoft Access Database Engine 2010 Redistributable

http://www.microsoft.com/download/en/details.aspx?id=13255

also you'll need to change the connexion string from “Provider=Microsoft.Jet.OLEDB.4.0; “ to “Provider=Microsoft.ACE.OLEDB.12.0;”

reference from my blog : 64 bit version of 'Microsoft.Jet.OLEDB.4.0' Office 2007/2010 Jet drivers

hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM