[IDC reconstruction] connectionstring property has not been initialized

Source: Internet
Author: User

Data Center reconstruction ing. In every study, we have always been with us, but we still have problems of both big and small. It is because of their existence that we have learned what we have learned, learned, and had fun.

Over the past few days, I have been studying the use of sqlhelper and generic collections. For a few days, it seems like a result today, but the result will never be easy.

As shown in the following figure:

Error message: the connectionstring property has not been initialized. Because of its appearance, I started my Baidu journey for more than an hour! Below

Let's summarize this question. For your reference, there is something wrong. If you want to see it, leave a comment!

Analysis:

The attribute has not been initialized. It indicates that it is not assigned a value before it is used.

Step 1.First, we can debug the breakpoint to see if it is assigned a value. After practice, we will find the result as follows:

Step 2.The error indicates that there may be an error in the data connection. Go to the configuration file to check whether the data connection string is correct.

Correct:

<?xml version="1.0" encoding="utf-8" ?><configuration>  <appSettings>    <add key= "Connstr" value="Server=hcy-PC;Database=Charge;User ID=sa;password=123456"/>  </appSettings></configuration>

After running again, connstr has a value, but the result is still incorrect.


Step 3.Check the written code for logical errors or writing errors.

Error example:In this way, a Conn is instantiated during execution, and a new Conn is created, and the above error message is displayed.

Of course, you should also pay attention to whether the first line of code is correct. From the data query, we found that this is a very prone to errors.

Private ReadOnly ConnStr As String = ConfigurationManager.AppSettings("ConnStr")  <span style="color:#ff0000;">  Dim Conn As New SqlConnection(ConnStr)</span>
<span style="color:#ff0000;">    Public Sub New()        Conn = New SqlConnection    End Sub</span>


Correct example:

First, initialize the conn directly when defining it.

<span style="font-size:18px;"><span style="font-family:KaiTi_GB2312;">Private ReadOnly ConnStr As String = ConfigurationManager.AppSettings("ConnStr") <span style="color:#333333;">   </span><span style="color:#ff0000;">Dim Conn As New SqlConnection(ConnStr) </span></span></span>

Type 2: Define first, and then define a new constructor to instantiate it.

<span style="font-size:18px;">Private ReadOnly ConnStr As String = ConfigurationManager.AppSettings("ConnStr")   <span style="color:#ff0000;"> Dim Conn As New SqlConnection     Public Sub New()        Conn = New SqlConnection(ConnStr)    End Sub</span></span>

The more errors we encounter, the more we learn from the problem solving process. What I learned is not only knowledge, but also attitude towards the problem, from fear at the beginning to accepting the problem and solving the problem. Why is this not an improvement?


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.