• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

The ConnectionString property has not been initialized. Why

The ConnectionString property has not been initialized. This error comes when one entry is being done. i.e on second entry.

Imports System
Imports System.Data
Imports System.Data.SqlClient


Public Class NewUsernamepasswords
    Dim con As New SqlConnection
    Dim da As SqlDataAdapter
    Dim ds As DataSet
    Dim dt As DataTable
    Dim s As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       
        ' Delete any existing record.
        Dim query As String
        Dim cmd As New SqlCommand
        query = "DELETE FROM accounts WHERE UserName='" & TextBox1.Text & "'"
        cmd = New SqlCommand(query, con)
        con.Open()
        cmd.ExecuteNonQuery()

        ' Insert the new record.
        query = "INSERT INTO accounts(username,password) VALUES ('" & TextBox1.Text & "', '" & HashPassword(TextBox2.Text) & "')"
        cmd = New SqlCommand(query, con)
        cmd.ExecuteNonQuery()

        ' Close the connection.
        con.Close()
        con.Dispose()
        query = ""

        MessageBox.Show("Ok")
    End Sub


    Private Sub NewUsernamepasswords_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con = New SqlConnection("server=ADMIN-PC\SQLEXPRESS;database=testadms;Integrated security=true")
    End Sub
    Private Function HashPassword(ByVal password As String) As String
        Dim hash As String = ""
        Dim i As Integer
        For i = 0 To password.Length - 1
            hash &= Chr(Asc(password.Substring(i, 1)) + 1)
        Next i
        Return hash
    End Function
End Class
0
searchsanjaysharma
Asked:
searchsanjaysharma
1 Solution
 
Paul JacksonCommented:
You dispose of the con object at the end of  Button1_Click, therby destroying the connection and the connection string.
Better to move the initialisation of the con object within  Button1_Click rather than in  NewUsernamepasswords_Load.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

         Dim con As New SqlConnection
         con = New SqlConnection("server=ADMIN-PC\SQLEXPRESS;database=testadms;Integrated security=true")

        ' Delete any existing record.
        Dim query As String
        Dim cmd As New SqlCommand
        query = "DELETE FROM accounts WHERE UserName='" & TextBox1.Text & "'"
        cmd = New SqlCommand(query, con)
        con.Open()
        cmd.ExecuteNonQuery()

        ' Insert the new record.
        query = "INSERT INTO accounts(username,password) VALUES ('" & TextBox1.Text & "', '" & HashPassword(TextBox2.Text) & "')"
        cmd = New SqlCommand(query, con)
        cmd.ExecuteNonQuery()

        ' Close the connection.
        con.Close()
        con.Dispose()
        query = ""

        MessageBox.Show("Ok")
    End Sub

Open in new window

0
 
searchsanjaysharmaAuthor Commented:
ok
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now