?
Solved

The ConnectionString property has not been initialized. Why

Posted on 2012-03-20
2
Medium Priority
?
246 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

801 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