Solved

Creating a web form using asp.net

Posted on 2015-02-09
4
183 Views
Last Modified: 2015-02-09
I am trying to create a web form which would add records to a Microsoft Access database (accbd format).
This will include several text boxes and drop down lists. But for the beginning I've created only two text boxes  - ID and Namef. Also, I' ve put a gridview item on the page so I can see changes in data as they occur.


After launching the page in a browser, filling the ID and Namef fields and pressing "submit" button I recieve and error message:

Screenshot---09-02-2015---12-31-03.png
My knowledge of asp.net and web programming in general is quite limited, so I based my form on code shown in that video:
https://www.youtube.com/watch?v=CKdQUgrL3Gg

My vb code is:

Imports System.Data.OleDb

Partial Class _Default
    Inherits System.Web.UI.Page

    

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim myConn As OleDbConnection
        Dim cmd As OleDbCommand
        Dim sqlString, ID, Namef As String

        ID = TxtID.Text
        Namef = txtNamef.Text

        myConn = New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=|DataDirectory|Database1.accdb;")

        myConn.Open()
        sqlString = "INSERT INTO Table1 (ID, Namef) VALUES ('" + ID + "', '" + Namef + "')"

        cmd = New OleDbCommand(sqlString, myConn)
        cmd.ExecuteNonQuery()
        myConn.Close()


    End Sub
End Class

Open in new window


I also attach an archive containing my project. The database is stored in App_Data folder.
Thanks in advance!
Attempt1.zip
0
Comment
Question by:German Mikulski
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Lokesh B R
Comment Utility
Hi,

As i can see in your Table, you have already set the primary Key as ID.

Please change the query as below and try.

 sqlString = "INSERT INTO Table1 (Namef) VALUES ('" + Namef + "')"

Open in new window

0
 

Author Comment

by:German Mikulski
Comment Utility
Hi,

Sorry, it didn't work, still getting the same mistake. Visual Studio points at  myConn.Open() line as at the one, where mistake occurs. Screenshots of table:
Screenshot---09-02-2015---12-53-57.pngScreenshot---09-02-2015---12-55-14.png
0
 
LVL 14

Accepted Solution

by:
Lokesh B R earned 500 total points
Comment Utility
Hi,

When you use  Access 2007 or higher files using OLEDB connection in C# or VB.Net, you need to install this.

http://www.microsoft.com/en-in/download/confirmation.aspx?id=23734

Code as below.

 
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim myConn As OleDbConnection
        Dim cmd As OleDbCommand
        Dim sqlString, Namef As String

        Namef = txtNamef.Text
 myConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb;Persist Security Info=True;")

        myConn.Open()

        sqlString = "INSERT INTO Table1 (Namef) VALUES ('" + Namef + "')"

        cmd = New OleDbCommand(sqlString, myConn)
        cmd.ExecuteNonQuery()
        myConn.Close()


    End Sub
End Class

Open in new window

0
 

Author Closing Comment

by:German Mikulski
Comment Utility
Thanks a lot! :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now