Solved

The ConnectionString property has not been initialized. Why

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with sorting data in Listbox using VB.NET 3 61
LINQ - C# to VB convertion 12 56
VB.Net Report Printing Issue 3 32
Create a datatable in vb.net dynamically 1 12
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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