ASP.NET Guestbook - Feeding the Database (Page 5 of 8 )
When the user clicks the Link Label (llPost), data posting starts. We handle it by implementing the LLPOST.CLICK event handler as given below:
Private Sub llPost_Click
(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles llPost.Click
Line1: Dim insertData As DataRow = DataSet11.Tables("feedback").NewRow 'create a new row
'populate the datarow with data from the form
Line2: insertData("name") = txtName.Text
Line3: insertData("email") = txtEmail.Text
Line4: insertData("comments") = txtComments.Text
Line5: insertData("rating") = CInt(ddnRating.SelectedItem.Value)
Line6: DataSet11.Tables("feedback").Rows.Add(insertData) 'add the populated row to the dataset
Try
Line7: SqlDataAdapter1.Update(DataSet11.Tables("feedback")) 'update the database
'always close the connection
Line8: SqlConnection1.Close()
Line9: Response.Redirect("viewcomments.aspx", True)
Line10: Catch ex As SqlClient.SqlException
'do something here
Line11: Catch ex As Exception
'do something here
Line12: End Try
End Sub
Line 1: creates a new data row to insert the post data
Line 2 - Line 5: Populates the data row with the inputs gathered from the users
Line 6: Adds the data row to the dataset(Dataset11)
Line 7: User inputs will not be fed to the database unless you call the adapter’s update method. We do it in this line
Line 8: Always close the connection. Note the fact that we have included Codes inside a try and catch block.
Line 9: Redirect the users another page
Line 10 – Line 11: Catch the exception
Next: Viewing Guestbook: viewcomments.aspx >>
More ASP.NET Code Articles
More By R. Abhiram Vikrant