Pages

Friday, February 11, 2011

Asp.net - inserting form data into a database

Here is a quick explanation for taking the information from a form throwing it into a database.

First let's import the libraries for connecting to SQL. These need to got at the very top of your code page.
Imports System.Data
Imports System.Data.SqlClient

Now the code. This will likely be placed in the click even of a button.
Dim sqlCon As New SqlConnection("Data Source=.\Sqlexpress;Initial Catalog=Sprinklers;Integrated Security=True")

Dim sql As String = "Insert into Sprinklers " & _
" (sprinklername,description) " & _
" values " & _
" (@sprinklername, @description) "

Dim sqlCommand As New SqlCommand(sql, sqlCon)
sqlCommand.Parameters.Add(New SqlParameter("@sprinklername", SqlDbType.VarChar, 50))
sqlCommand.Parameters("@sprinklername").Value = txtName.Text
sqlCommand.Parameters.Add(New SqlParameter("@description", SqlDbType.VarChar, 500))
sqlCommand.Parameters("@description").Value = txtDesc.Text
sqlCommand.Connection.Open()
Try
sqlCommand.ExecuteNonQuery()
Catch ex As SqlException
MsgBox(ex.Message)
End Try

sqlCommand.Connection.Close()
sqlCommand = Nothing
sqlcon = Nothing



Now for the code with comments. This will explain line by line what is happening.
     ' We need to tell our program how to get to the database
Dim sqlCon As New SqlConnection("Data Source=.\Sqlexpress;Initial Catalog=Sprinklers;Integrated Security=True")

' Here is the statement that inserts data into the database.
' Notice the @sprinklername type variable. We will use a parameters
' feature to replace those with our form values
Dim sql As String = "Insert into Sprinklers " & _
" (sprinklername,description) " & _
" values " & _
" (@sprinklername, @description) "

' The sql connection told our program where to go,
' this tells the program what to do when it gets there
' We are passing in the sql statement and the connection we created before
Dim sqlCommand As New SqlCommand(sql, sqlCon)

' Now its time to give some values to those @ variables
' First let's tell the program which param we are dealing with, @sprinklername in this case
' The sqldbtype and the last number in the variables here need to match the datatype
' and length that are set for the column we are inserting into.
sqlCommand.Parameters.Add(New SqlParameter("@sprinklername", SqlDbType.VarChar, 50))
' Now that we've added a new parameter, let's give it a value.
sqlCommand.Parameters("@sprinklername").Value = txtName.Text

' Do it again for the other variable
sqlCommand.Parameters.Add(New SqlParameter("@description", SqlDbType.VarChar, 500))
sqlCommand.Parameters("@description").Value = txtDesc.Text

' Our statement is fully prepared and we need to open the connection.
sqlCommand.Connection.Open()

Try
' This will do the actual execution of the query. We want this inside of
' a try in case something goes wrong.
sqlCommand.ExecuteNonQuery()
Catch ex As SqlException
MsgBox(ex.Message)
End Try

' Close the connection to the database
sqlCommand.Connection.Close()

' Do away with the connection and command for memory management.
sqlCommand = Nothing
sqlCon = Nothing

No comments:

Post a Comment