Imports System.Data
Imports System.Data.SqlClient

Partial Class test
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim txtName As String = TextName.Text
        Dim txtcomment As String = TextComment.Text
        Dim txtemail As String = TextEmail.Text
        Dim con As SqlConnection
        Dim cmd As SqlCommand

        con.ConnectionString = "SqlData Source=(LocalDB)\v11.0;AttachDbFilename=App_Data\library.mdf; Integrated Security=True;Connect Timeout=30"
        cmd.Connection = con

        con.Open()
        cmd.CommandText = "INSERT INTO table([Name], [Comment], [emailaddress]) VALUES([txtname], [txtcomment], [txtemail])"
        cmd.ExecuteNonQuery()

        con.Close()

    End Sub
End Class

i am getting 'nullreferenceexecption was unhandled by user code' error in this code

Add a try catch block around ur sql statements and the insert sql is not correct

 cmd.CommandText = "INSERT INTO table([Name], [Comment], [emailaddress]) VALUES('" + txtname + "','" + txtcomment+ "','" + txtemail + "')"

In the line

cmd.CommandText = "INSERT INTO table([Name], [Comment], [emailaddress]) VALUES([txtname], [txtcomment], [txtemail])"

you haven't supplied any values for the fields. The actual query needs data as in

cmd.CommandText = "INSERT INTO table([Name], [Comment], [emailaddress]) " &
                   VALUES('Fred','some guy','fred@hotmail.com')"

or

cmd.CommandText = "INSERT INTO table([Name], [Comment], [emailaddress]) VALUES(" &
                  "'" & txtName.Text    & "'," &
                  "'" & txtComment.Text & "'," &
                  "'" & txtEmail.Text   & "')"

although to be safe you should use parameterized entry to avoid SQL injection attacks as in

cmd.CommandText = "INSERT INTO table([Name], [Comment], [emailaddress]) VALUES(@name,@comment,@email)"
cmd.Parameters.AddWithValue("@name","Fred")
cmd.Parameters.AddWithValue("@comment","some guy")
cmd.Parameters.AddWithValue("@email","fred@hotmail.com")

Edited 4 Years Ago by Reverend Jim

i am getting error:
** Variable 'con' is used before it has been assigned a value. a null reference exception could result at runtime.
**
and for
** Variable 'cmd' is used before it has been assigned a value. a null reference exception could result at runtime.
**

Imports System.Data
Imports System.Data.SqlClient

Partial Class test
    Inherits System.Web.UI.Page


    Dim con As SqlConnection
    Dim cmd As SqlCommand

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click


        con.ConnectionString = "Data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
        cmd.Connection = con

        con.Open()
        cmd.CommandText = "INSERT INTO table([Name], [Comment], [emailaddress]) VALUES(" &
                  "'" & TextName.Text & "'," &
                  "'" & TextComment.Text & "'," &
                  "'" & TextEmail.Text & "')"
        cmd.ExecuteNonQuery()

        con.Close()

    End Sub
End Class

Object reference not set to an instance of an object.
i am facing this error on con.ConnectionString="Data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"

It means you are trying to use an object before it was created. When you declare

Dim con As SqlConnection

you are saying "I want to create a variable that can be used to refer to a SqlConnection object". That doesn't actually create the object., however if you do

Dim con As New SqlConnection

That creates the reference AND creates the object. It is shorthand for

Dim con As SqlConnection
con = New SqlConnection

The first statement creates a variable (con) that can refer to the object. The second statement creates the object (allocates memory) and assigns the address of that object to con.

It's like going to the post office and saying "I want to reserve an address". That doesn't automatically cause a house to be built. However, if you then build a house at 123 Elm Street, then go to the post office and register that address you can now get mail sent to that location.

"Object reference not set to an instance of an object" would be the equivalent of "Return to sender - no such address".

Edited 4 Years Ago by Reverend Jim

This question has already been answered. Start a new discussion instead.