Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > Visual Web Developer 2005
|
Visual Web Developer 2005 Discuss creating ASP.NET 2.0 sites with Microsoft's Visual Web Developer 2005. If your question is more specific to a piece of code than the Visual tool, see the ASP.NEt 2.0 forums instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Web Developer 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 15th, 2009, 12:24 PM
Authorized User
 
Join Date: Jan 2009
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
Default Inserting values into Access Database using OLE

Scenario
When user click on Button1(which is a submit button), 2 values will be pass into the database -name1.Text and nric1.Text.

The code doesn't haven any error but however, it doesn't add into the database. Strange.

File coding
Imports System.Data.OleDb

Partial Class UpdateEmployee
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim name As String = name1.Text
Dim nric As String = nric1.Text

Dim myConnectionString As String
myConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=C:\Documents and Settings\Guohao Chen\Desktop\WebSite7\App_Data\WSDatabase.mdb"
Dim myConnection As New OleDbConnection(myConnectionString)
Dim myQuery As String = "insert into Employee(Empname,Empnric) values('" + name + "','" + nric + "')"
Dim myCommand As New OleDbCommand(myQuery)

'open the database
myCommand.Connection = myConnection
myConnection.Open()

'close connection
myCommand.Connection.Close()
End Sub
End Class
 
Old January 15th, 2009, 06:12 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Did you debug???

One good place to place a breakpoint would be here:
Code:
Dim myCommand As New OleDbCommand(myQuery)
Then view the value of the myQuery variable and see if the query makes sense.

I would note that you are *NOT* protecting yourself against SQL Injection (admittedly not a huge problem with OLE DB limitations) and you aren't allowing a name of (for example) O'Brien

You'd be better off using a parameterized command, but at a minimum you should do
Code:
Dim name As String = Replace(name1.Text,"'","''")
        Dim nric As String = Replace(nric1.Text,"'","''")
Also, as a minor point, the proper string concatenation operator in VB is the ampersand (&). You can use + but you are at the whim (okay the rules) of VB as to whether it does arithmetic or concatenation.

But anyway, start by debugging.





Similar Threads
Thread Thread Starter Forum Replies Last Post
having problem in inserting in access database alto ASP.NET 2.0 Basics 4 May 24th, 2007 08:57 PM
Inserting data into Access database OldCoder ASP.NET 2.0 Basics 1 December 31st, 2005 06:26 PM
Error while inserting values into database table pothireddy_s SQL Server 2000 2 August 3rd, 2005 03:01 AM
Inserting Values in Database OracleCommand VB.NET narendra_patil BOOK: Beginning ASP.NET 1.0 0 April 22nd, 2005 07:35 AM
inserting values into a database RPG SEARCH ASP.NET 1.0 and 1.1 Basics 3 February 5th, 2005 07:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.