C# ConnectionString属性尚未初始化。窗口应用程序

C# ConnectionString属性尚未初始化。窗口应用程序,c#,visual-studio,C#,Visual Studio,我得到了错误 The ConnectionString property has not been initialized. 我的窗口应用程序代码如下&格式正确,但在提交数据后仍会出错 我使用一个本地数据库(我自己的本地PC)作为表 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.L

我得到了错误

The ConnectionString property has not been initialized.
我的窗口应用程序代码如下&格式正确,但在提交数据后仍会出错

我使用一个本地数据库(我自己的本地PC)作为表

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace Tag_Number
{
    public partial class Form1 : Form
    {
        string DBConn;
        protected void Page_Load(object sender, EventArgs e)
        {
            DBConn = ConfigurationManager.ConnectionStrings[@"C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf"].ConnectionString;

        }

        int InsertProduct()
        {
            using (SqlConnection myConnection = new SqlConnection(DBConn))
            {
                SqlCommand MyCommand = new SqlCommand("INSERT INTO NEW_SO_TAG_NUMBER (SOLine, SerialNbr, StatusCode, PackType, PalletID, PackingListNo) Values (@SOLine, @SerialNbr, @StatusCode, @PackType, @PalletID, @PackingListNo)", myConnection);
                MyCommand.Parameters.AddWithValue("@SOLine", sOLineTextBox.Text);
                MyCommand.Parameters.AddWithValue("@SerialNbr", serialNbrTextBox.Text);
                MyCommand.Parameters.AddWithValue("@StatusCode", statusCodeComboBox.Text);
                MyCommand.Parameters.AddWithValue("@PackType", packTypeComboBox.Text);
                MyCommand.Parameters.AddWithValue("@PalletID", palletIDTextBox.Text);
                MyCommand.Parameters.AddWithValue("@PackingListNo", palletIDTextBox.Text);
                myConnection.Open();
                return MyCommand.ExecuteNonQuery();

            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Bla Bla Bla.",
        "Info",
        MessageBoxButtons.OK,
        MessageBoxIcon.Information,
        MessageBoxDefaultButton.Button1);
        }

        private void nEW_SO_TAG_NUMBERBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.nEW_SO_TAG_NUMBERBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.tag_NumbersDataSet);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'tag_NumbersDataSet.NEW_SO_TAG_NUMBER' table. You can move, or remove it, as needed.
            this.nEW_SO_TAG_NUMBERTableAdapter.Fill(this.tag_NumbersDataSet.NEW_SO_TAG_NUMBER);

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                serialNbrTextBox.ReadOnly = false;
                MessageBox.Show("Remember to fill in your Bla Bla Bla.","Remind",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information,
                MessageBoxDefaultButton.Button1);
            }
            else
            {
                serialNbrTextBox.ReadOnly = true;
            }
        }

        private void packTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void statusCodeLabel_Click(object sender, EventArgs e)
        {

        }

        private void statusCodeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void packingListNoLabel_Click(object sender, EventArgs e)
        {

        }

        private void packingListNoTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void palletIDLabel_Click(object sender, EventArgs e)
        {

        }

        private void palletIDTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void serialNbrLabel_Click(object sender, EventArgs e)
        {

        }

        private void serialNbrTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void sOLineLabel_Click(object sender, EventArgs e)
        {

        }

        private void sOLineTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void packTypeLabel_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            InsertProduct();
        }
    }
}

在我插入需要放入表中的数据后,它会弹出此错误。

第行下方不会返回连接字符串

ConfigurationManager.ConnectionString[@“C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Number.sdf”]。ConnectionString

如果您的配置文件中有名为
Target
的连接字符串,如下所示

<connectionStrings>
   <add name="Target"

尝试以下方法:

  • 试着说出你的连接字符串。名称:不正确
  • 显示您的应用程序配置文件
  • 您确定调用了页面加载吗。(ASP.Net与Windows窗体不同)

  • 只需将代码从Page_Load函数中转换为初始化DBConn对象

        protected void Page_Load(object sender, EventArgs e)
        {
            DBConn = ConfigurationManager.ConnectionStrings[@"C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf"].ConnectionString;
    
        }
    
    要形成1_加载函数

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'tag_NumbersDataSet.NEW_SO_TAG_NUMBER' table. You can move, or remove it, as needed.
            DBConn = ConfigurationManager.ConnectionStrings[@"C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf"].ConnectionString; //Do it here.....
            this.nEW_SO_TAG_NUMBERTableAdapter.Fill(this.tag_NumbersDataSet.NEW_SO_TAG_NUMBER);
    
        }
    

    顾名思义,页面加载函数是System.Web.UI的一部分,但您使用的是System.Windows.Forms,因此在将事件侦听器显式添加到
    FormLoad
    事件之前,不会调用页面加载函数。默认情况下,在System.Windows.Forms中,通过单击加载事件生成的函数是Form\u load,但您也可以更改为Page\u load函数。

    那么,DBConn在分配后有什么值?您预计何时调用
    Page\u load
    ?正在加载的网页是什么?这是一个windows窗体类吗?您还需要连接事件。@ta.speot.is我相信Form1\u load函数会通过单击windows中的事件生成,但您有有效点。这纯粹是为了避免OP将
    Page\u load
    重命名为
    Form1\u load
    ,然后想知道为什么没有编译/工作/随便什么。
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'tag_NumbersDataSet.NEW_SO_TAG_NUMBER' table. You can move, or remove it, as needed.
            DBConn = ConfigurationManager.ConnectionStrings[@"C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf"].ConnectionString; //Do it here.....
            this.nEW_SO_TAG_NUMBERTableAdapter.Fill(this.tag_NumbersDataSet.NEW_SO_TAG_NUMBER);
    
        }