Monday, March 26, 2012

Please Help

I am writing a guest book for a web page that I am developing at the moment. The guest book is kept in an access database.

I can get the data out alright and display it, however the problem occurs when the user goes to add a comment.

Everything works fine. The user puts all the info in and then clicks on the submit button. It seems that it works. But when the page refreshes the data is not there.

I think there must be something wrong with the add function.

Could some one please help me out as I am at the end of my witts.

The code is below. Cheers,

Public Function Add(ByVal Author As String, ByVal Email As String, _
ByVal Homepage As String, ByVal State As String, ByVal Comment As String) As Boolean
Dim sql As String = "INSERT INTO Comments (Author, Email, Homepage, State, Comment) "
sql += "VALUES (@dotnet.itags.org.Author, @dotnet.itags.org.Email, @dotnet.itags.org.Homepage, @dotnet.itags.org.State, @dotnet.itags.org.Comment)"
' create a new OleDbCommand and set its params
Dim myCmd As OleDbCommand = New OleDbCommand(sql, _Connection)
myCmd.Parameters.Add(New OleDbParameter("@dotnet.itags.org.Author", OleDbType.VarChar, 50))
myCmd.Parameters("@dotnet.itags.org.Author").Value = Author.Trim()
myCmd.Parameters.Add(New OleDbParameter("@dotnet.itags.org.Email", OleDbType.VarChar, 50))
myCmd.Parameters("@dotnet.itags.org.Email").Value = Email.Trim()
myCmd.Parameters.Add(New OleDbParameter("@dotnet.itags.org.Homepage", OleDbType.VarChar, 100))
myCmd.Parameters("@dotnet.itags.org.Homepage").Value = Homepage.Trim()
myCmd.Parameters.Add(New OleDbParameter("@dotnet.itags.org.State", OleDbType.VarChar, 50))
myCmd.Parameters("@dotnet.itags.org.State").Value = State.Trim()
myCmd.Parameters.Add(New OleDbParameter("@dotnet.itags.org.Comment", OleDbType.VarChar))
myCmd.Parameters("@dotnet.itags.org.Comment").Value = EncodeHTMLText(Comment.Trim())
Add = True
myCmd.Connection.Open()
Try
myCmd.ExecuteNonQuery()
Catch e As OleDbException
Add = False
Finally
myCmd.Connection.Close()
End Try
End FunctionHave you tried using ? instead of @.paramname:-

Dim sql As String = "INSERT INTO Comments (Author, Email, Homepage, State, Comment) "
sql &= "VALUES (?, ?, ?, ?, ?)"
I gave up and re wrote the form using ADODB. My code is below. If any one has any ways to improve please tell me.

Thankx for your help....

Dim rs As ADODB.Recordset
rs = New ADODB.Recordset()
rs.CursorType = ADODB.CursorTypeEnum.adOpenKeyset
rs.LockType = ADODB.LockTypeEnum.adLockOptimistic
rs.Open("Comments", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\webspace\myweb.com\db\Guestbook.mdb;Persist Security Info=False;", , , ADODB.CommandTypeEnum.adCmdTable)
rs.AddNew()
rs.Fields(1).Value = Author.Text
rs.Fields(2).Value = Email.Text
rs.Fields(3).Value = Homepage.Text
rs.Fields(4).Value = State.Text
rs.Fields(5).Value = Comment.Text
rs.Update()
rs.Close()
rs = Nothing

0 comments:

Post a Comment