65.9K
CodeProject is changing. Read more.
Home

Performing DataBase Entry

Aug 9, 2002

viewsIcon

60695

downloadIcon

987

To illustrate record entry in to a SQL Database

Sample Image - DbEntry.jpg

Introduction

This article demonstrates the use of SqlConnection and SqlAdapter to login to a page and make data entry in to a SQL Database.

This was coded in ASP.NET . The application opens with a login page and no validation mechanism is provided to check the user validity and the page navigates into another page , where the user is allowed to make an entry in to a form which asks a name and a phone number. The input data is stored in a database which is created using SQL Server.

A sample code will explain how it is performed.

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        TextBox1.TextMode = TextBoxMode.Password
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (TextBox1.Text <> "" & TextBox2.Text <> "") Then
            Response.Redirect("Introduction.aspx?name=" & System.Web.HttpUtility.UrlEncode(TextBox2.Text & " " & TextBox1.Text))
        Else
            Response.Write("Please Enter a valid Name and Password")
        End If
    End Sub

The following code performs insertion into the database

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (TextBox1.Text <> "" And TextBox2.Text <> "") Then
         
            Dim MyConnection = New System.Data.SqlClient.SqlConnection("server=(local)\NetSDK;database=PhoneDatabase;Trusted_Connection=yes")

            Dim MyCommand As System.Data.SqlClient.SqlCommand
            Dim InsertCmd As String = "insert into Phone values('" + TextBox1.Text + "'," + TextBox2.Text + ")"
'requires validation to be performed. None done here
            MyCommand = New System.Data.SqlClient.SqlCommand(InsertCmd, MyConnection)
            MyCommand.Connection.Open()

            MyCommand.ExecuteNonQuery()
            MyCommand.Connection.Close()
        End If
        Response.Redirect("Introduction.aspx")
    End Sub