Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My window application code is like below & the format is correct, but still i get error after i submit my data.

im using a LocalDataBase (My own local PC) for the table.

C#
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();
        }
    }
}
Posted
Comments
BulletVictim 30-Oct-13 4:13am    
Post your App.Config where you declare "ConnectionStrings"

Hello,

The ConfigurationManager.ConnectionStrings returns a ConnectionStringSettingsCollection object that contains the contents of the ConnectionStringsSection object for the current application's default configuration. Make sure that your application configuration file contains the necessary entries. The typical configuration is similar to the one shown below.
XML
<configuration>
  <connectionstrings>
    <add name="ConnStr1" connectionstring="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb">
      providerName="System.Data.SqlClient" />
  </add></connectionstrings>
</configuration>

More information on this can be found in MSDN Library[^].

Regards,
 
Share this answer
 
You are using wrong connection string.
Find a connection string if you are using visual studio in easy way
database connectivity using visual studio
Connection String from Visual Studio

choose Microsoft SQL server database File in options.
 
Share this answer
 
Most likely, you need to check your web.config:
C#
DBConn = ConfigurationManager.ConnectionStrings[@"C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf"].ConnectionString;
tries to retrieve a string with a the name
C:\Users\MyName\Documents\Visual Studio 2012\Projects\Tag Number\Tag Number\Tag Numbers.sdf
rather than a value - normally, strings would be called "TagNumbers" or similar and the content tells the system how to connect to the file.

But there is a much more fundamental problem here: SDF files are not something SQL uses: you need to use a Database on the SQL server system itself, or you will get horrible, horrible problems in future (SDF files are not multi user and it will all start to fall apart when you get more than one user as a result)
 
Share this answer
 
Comments
BulletVictim 30-Oct-13 4:12am    
OriginalGriff you are correct the most likely problem is with his App.config (rather than web.config since this is a windows forms application).
OriginalGriff 30-Oct-13 4:45am    
Ah yes - the use of "Page_Load" threw me - I need more caffeine... :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900