我得到了错误。
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();
}
}
}
在我插入需要放入表中的数据后,它会一直弹出此错误。