Solved

The ConnectionString property has not been initialized. Why

Posted on 2012-03-20
2
237 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
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now