Solved

The ConnectionString property has not been initialized. Why

Posted on 2012-03-20
2
245 Views
Last Modified: 2012-03-20
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
Comment
Question by:searchsanjaysharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 37741826
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
 

Author Closing Comment

by:searchsanjaysharma
ID: 37745821
ok
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question