Imports System.Data.SqlClient
Imports System.Web.Configuration

Partial Class postformread
    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 id As Integer
        Dim theName As String
        Dim theAge As Integer
        Dim theCourse 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("studentname")
        theAge = Request.Form("studentage")
        theCourse = Request.Form("studentcourse")

        Try
            connection = New SqlConnection(WebConfigurationManager.ConnectionStrings("StudentConnectionString").ConnectionString)
            command = New SqlCommand("INSERT INTO StudentTable(Name,Age,Course) VALUES(@Name, @Age, @Course)", connection)
            command.Parameters.AddWithValue("@Name", theName)
            command.Parameters.AddWithValue("@Age", theAge)
            command.Parameters.AddWithValue("@Course", theCourse)

            Using connection
                connection.Open()
                result = command.ExecuteNonQuery()
                command.CommandText = "SELECT @@IDENTITY"
                id = Convert.ToInt32(command.ExecuteScalar())
                Response.Write("Customer added, ID is " & id)
                connection.Close()
            End Using
        Catch
            Response.Write("Error. Customer not added")
        End Try

    End Sub
End Class
