0

我得到了错误。

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();
        }
    }
}

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

4

3 回答 3

0

尝试以下操作:

  1. 尝试给出你的连接字符串。名称:<> 不正确。
  2. 向我们展示您的应用程序配置文件。
  3. 您确定调用了 Page_Load 吗?(ASP.Net 不同于 Windows 窗体)
于 2013-10-30T08:11:04.507 回答
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,因此在您将事件侦听器显式添加到事件之前,不会调用 Page_Load 函数FormLoad。默认情况下,在 System.Windows.Forms 中,单击加载事件生成的函数是Form_Load ,但您也可以更改为 Page_Load 函数。

于 2013-10-30T08:15:18.380 回答
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))
{

}
于 2013-10-30T08:09:24.493 回答