locked
ExecuteNonQuery: Connection property has not been initialized. RRS feed

  • Question

  • well, i have written code of an address book.But i am not able to input consecutive entries after entering first entry when i save the next one i get the following error "ExecuteNonQuery: Connection property has not been initialized."
    code:-
    Imports System.Data
    Imports System.Data.SqlClient
    Public Class address_book
        Dim conn As New SqlClient.SqlConnection
        Dim adapter As New SqlDataAdapter
        Dim dsaaa As New DataSet
        Dim mytable As DataTable
        Dim rownumber As Integer
        Dim combuilder As SqlCommandBuilder
        Dim ds As DataSet

        Private Sub address_book_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            conn.ConnectionString = "server=localhost;uid=sa;database=directory"
            adapter = New SqlDataAdapter("SELECT * from aaa", conn)
            conn.Open()
            adapter.Fill(dsaaa, "aaa")
            'conn.Close()
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim iteminfopad As New SqlClient.SqlCommand("Insert into aaa(name,flat_no,name_building,road,city,pincode,office_no,residence_no,mobile,email_id) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "','" & TextBox7.Text & "','" & TextBox8.Text & "','" & TextBox9.Text & "','" & TextBox10.Text & "')", conn)
            iteminfopad.ExecuteNonQuery()
            conn.Close()
            conn = Nothing
            'TextBox11.Text = "      DATA HAS BEEN SAVED!!!" + vbCrLf + vbCrLf + vbCrLf + "NAME:-" + TextBox1.Text.ToString + vbCrLf + "ADDRESS:-" + TextBox2.Text.ToString + " , " + TextBox3.Text.ToString + vbCrLf + "                       " + TextBox4.Text.ToString + vbCrLf + "                       " + TextBox5.Text.ToString + "-" + TextBox6.Text.ToString + vbCrLf + "PHNNO. OFFICE:-" + TextBox7.Text.ToString + vbCrLf + "        RESIDENCE:-" + TextBox8.Text.ToString + vbCrLf + "                  MOBILE:-" + TextBox9.Text.ToString + vbCrLf + "EMAIL-ID:-" + TextBox10.Text.ToString + vbCrLf
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox3.Text = ""
            TextBox4.Text = ""
            TextBox5.Text = ""
            TextBox6.Text = ""
            TextBox7.Text = ""
            TextBox8.Text = ""
            TextBox9.Text = ""
            TextBox10.Text = ""
            TextBox11.Text = ""
            TextBox1.Focus()
            'conn.Open()
        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            data.Show()
        End Sub
    End Class
    Friday, August 8, 2008 12:10 PM

Answers

  • You are doing

    conn = Nothing

    in the button click event. Obviously the next time the button click event runs, variable conn will be empty.
    Saturday, August 9, 2008 1:27 AM

All replies

  • You are doing

    conn = Nothing

    in the button click event. Obviously the next time the button click event runs, variable conn will be empty.
    Saturday, August 9, 2008 1:27 AM
  • Thanks Michal!!!
    Saturday, August 9, 2008 8:32 AM