You don't need all those DataSets and DataRows for a simple insert to your database. This looks very long but that's because I have left your code in but commented it out, so you can compare the differences. I have tried to explain with comments all the details. I am a C# programmer so my VB may not be perfect and you might have to fix some of the syntax. If you do make changes please post them back here for me so I can see them.
sub Add(obj as object, e as EventArgs)
'dim objConn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\users\database\users.mdb")
'Use Server.MapPath function to get the physical path to the .mdb database file
dim objConn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/users/database/users.mdb"))
objConn.Open()
'dim objCmd as new OleDbDataAdapter("select * from contacts", objConn)
'Create an SQL insert statement I don't know what the column names should be so I have guessed
dim sql as String
sql = "insert into contacts (firstname, lastname) values('" & txtFirstName.text & "', '" & txtLastName.Text & "')"
'Create an adapter
dim objAdapter as new OleDbDataAdapter()
'set the adapters insert command with your SQL statement and connection object
objAdapter.InsertCommand = new OleDbCommand(sql, objCon)
'dim ds as DataSet = new DataSet()objCmd.Fill(ds, "contacts")
'dim dr as DataRow = ds.Tables("contacts").NewRow()dr(1)=txtFirstName.textdr(2)=
'ds.Tables("contacts").Rows.Add(dr)
'objCmd.Update(ds, "contacts")
'prepare a variable for the result of the databse transaction this could be used for error handling
dim rowsAffected as integer
'Execute the SQL statement, we would expect rowsAffected to be 1 for one record inserted
rowsAffected = objAdapter.InsertCommand.ExecuteNonQuery()
'dispose of your objects, yes there is a garbage collecter but that's no excuse to be lazy
objAdapter.Dispose()
objConn.Close()
objConn.Dispose()
end sub
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
Well it doesn't like the SQl insert statement, please post your modified code in code tags so I can look at it.
What are the column names in the database for first name and second name?
What other colmns are there ? do they allow null values or do we need to supply values for those too ?
Hmm access database it maybe we just need a semi-colon on the end of the SQL statement replace it with this
sql = "insert into contacts (firstname, lastname) values('" & txtFirstName.text & "', '" & txtLastName.Text & "');"
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
I changed the column names in the database so that they match with
your ones.
Oh well not the conventional method, but that's ok, normally you would change the code to fit the column names. But nevermind as long as they match that's the main thing.
As for the Username column, you can supply a zero-length string ('') that's not the same as a NULL value which is literally nothing.
So if the nullability of the username column is the problem try this insert statement instead.
sql = "insert into contacts (username, firstname, lastname) values('', '" & txtFirstName.text & "', '" & txtLastName.Text & "')"
what worries me slightly though is if nullability of the username column is the problem I would not expect a syntax errorSyntax error in INSERT INTO statement
I would expect a "column 'username' cannot be NULL" or something like that.
A final word the ; is oficially the statement terminator for SQL, I know MS SQL Server is not bothered about it, but I couldn't remember if Access was so forgiving.
Another point worth making is that your 'candidate key' (the column best suited to uniquely identify each row in the table and therefore be the primary key) is the username which would typically be the users email address, the AutoID is unecessary and meaningless, also a primary key can be a combination of columns it doesn't have to be just one. But don't worry about that too much let's just get it working first.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
But still thedatabase is not updated.
Are you trying to UPDATE an *existing* record or INSERT a *new* record?
You will have to post your code again I'm afraid in it's current state, because now there are no syntax errors, you now have a logic error because your code is not doing what you expect.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68