Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

database - "The ConnectionString property has not been initialized" error in VB.NET

Every time I tried to connect to the database it give me this error "The ConnectionString property has not been initialized"

what can I do to solve this?

here are my codes

Module Module1
    Function GetInfoForStudent(ByRef QueryName As String, ByVal UserName As String, ByVal Password As String) As DataTable
        Using Con As New SqlConnection
            Try
                Using OleCon As New SqlConnection
                    Dim Connection As String = "Data Source=.SQLEXPRESS;AttachDbFilename=G:VB ProjectLibrary
Catalog SystemLibrary Catalog Systemlibrary.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True"
                    Con.Open()
                    Dim Cmd As SqlCommand = Con.CreateCommand()
                    Cmd.CommandType = CommandType.StoredProcedure
                    Cmd.CommandText = QueryName
                    Cmd.Parameters.AddWithValue("@user", UserName)
                    Cmd.Parameters.AddWithValue("@pass", Password)
                    Dim da As New SqlDataAdapter(Cmd)
                    Dim ds As New DataTable()
                    da.Fill(ds)
                    Return ds
                End Using
            Catch ex As Exception

                Throw New Exception(ex.Message)
            End Try
        End Using

    End Function

End Module

Sub ShowStudentInfo()
    Dim dt As DataTable = GetInfoForStudent("MyStoredProcName", "@user", "@pass")
    ' Since (presumably) only one is returned
    With dt.Rows(0)
        ' Assign your text boxes 
        StudentIDTextBox.Text = .Item("StudentID")
        LoginIDTextBox.Text = .Item("LoginID")
        Student_NameTextBox.Text = .Item("Student Name")
        Student_addressTextBox.Text = .Item("Student address")

    End With
End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You never assigned your connection string to the connection object, just like the error is saying.

Insert a line setting the connection string before con.open.

Con.connectionstring = connection
Con.Open()

Or better yet, change your using statement as follows

Dim Connection As String = "Data Source=.SQLEXPRESS;AttachDbFilename=G:VB ProjectLibrary Catalog SystemLibrary Catalog Systemlibrary.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True"

Using Con As New SqlConnection(connection)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...