Imports System.Data.SqlClient
Imports System.Web.Configuration

Partial Class InsertIntoDatabase
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim result As Integer
        Dim theName As String

        ' Set the content type
        Response.ContentType = "text/plain"
        Response.ContentEncoding = Encoding.UTF8

        ' Read the information posted to this page from the form
        theName = Request.Form("thename")

        Try
            connection = New SqlConnection(WebConfigurationManager.ConnectionStrings("DatabaseConnectionString").ConnectionString)
            command = New SqlCommand("INSERT INTO Names(Name) VALUES(@Name)", connection)
            command.Parameters.AddWithValue("@Name", theName)

            Using connection
                connection.Open()
                result = command.ExecuteNonQuery()
                connection.Close()
            End Using

        Catch
            Response.Write("Error. Name not added")
        End Try

        ' For this example, I want to go back to the page
        ' displaying the database and the form that got
        ' me here.
        Response.Redirect("./Basic Ajax Tutorial 7 Polling Again.htm")
    End Sub
End Class
