I have an ms-access database called db.mdb and it contains various table. I;m creating a Account Creation page with ASP.NET and VB.NET.
I’m trying to input fields within the page into ms-acess db but when I open the db there are no values entered despite web develop not showing any errors.
Can you kindly help me?
This is the code:
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub TextBox1_TextChanged(sender As Object, e As System.EventArgs) Handles txtName.TextChanged
End Sub
Protected Sub btnCreateAccount_Click(sender As Object, e As System.EventArgs) Handles btnCreateAccount.Click
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Brian\Documents\Visual Studio 2010\WebSites\WebSite3\db.mdb;User Id=admin;Password=;")
Dim cmd As OleDbCommand = New OleDbCommand("INSERT INTO [USER] (UserName, UserSurname, Address, Country, TelNo, UserLogin, UserPassword, UserTypeID) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", conn)
If txtPass.Text = txtCPass.Text Then
conn.Open()
cmd.Parameters.Add("@UserName", OleDbType.VarChar, 255).Value = txtName.Text
cmd.Parameters.Add("@UserSurname", OleDbType.VarChar, 255).Value = txtSurname.Text
cmd.Parameters.Add("@Address", OleDbType.VarChar, 255).Value = txtUsername.Text
cmd.Parameters.Add("@Country", OleDbType.VarChar, 255).Value = txtCountry.Text
cmd.Parameters.Add("@TelNo", OleDbType.Integer).Value = txtTelNo.Text
cmd.Parameters.Add("@UserLogin", OleDbType.VarChar, 255).Value = txtUsername.Text
cmd.Parameters.Add("@UserPassword", OleDbType.VarChar, 255).Value = txtPass.Text
cmd.Parameters.Add("@UserTypeID", OleDbType.VarChar, 255).Value = "U"
cmd.ExecuteNonQuery()
conn.Close()
lblAccount.Visible = True
End If
End Sub
End Class
EDIT:
So I’m trying to autonumber the UserID field. It’s set as auto number from the access database. Whenever I try to input the details into the page from the page, it won’t allow me, giving me an error that the AutoNumber field cannot be NULL. So I added the piece of code :
newID = Int32.Parse(cmd2.ExecuteScalar()) + 1
So that I get the maximum number from that column in the table and increment it by 1 and then added with the other command.
cmd.Parameters.Add("@UserID", OleDbType.VarChar, 255).Value = newID
However its giving me an error saying
Conversion from type 'DBNull' to type 'String' is not valid.
You haven’t open the connection before calling cmd2.ExecuteScalar() function. And first parameter is wrong. Should be @UserID instead of @Address.