ConnectionString属性尚未初始化。窗口应用

时间:2013-10-30 08:04:17

标签: c# visual-studio

我收到了错误。

The ConnectionString property has not been initialized.

我的窗口应用程序代码如下&格式是正确的,但我提交数据后仍然会收到错误。

我使用LocalDataBase(我自己的本地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();
        }
    }
}

在我插入需要放入表格的数据之后,它会一直弹出这个错误。

3 个答案:

答案 0 :(得分:0)

下面的行不会返回连接字符串

ConfigurationManager.ConnectionStrings[@"C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf"].ConnectionString;

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

<connectionStrings>
   <add name="Target"

您可以通过名称

获取ConnectionString
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Target"].ConnectionString))
{

}

答案 1 :(得分:0)

尝试以下方法:

  1. 尝试发出连接字符串。名称:&lt;&gt;是不对的。
  2. 向我们展示您的Application-Config-File。
  3. 您确定要调用Page_Load吗? (ASP.Net与Windows-Forms不同)

答案 2 :(得分:0)

只需移动代码即可从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;

    }

到Form1_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);

    }

名称建议的 Page_Load 函数是 System.Web.UI 的一部分,但您使用的是 System.Windows.Forms ,所以在向FormLoad事件显式添加事件侦听器之前,不会调用Page_Load函数。默认情况下,在System.Windows.Forms中,单击加载事件生成的函数是 Form_Load ,但您也可以更改为Page_Load函数。