object reference not set to an instance of an object

I'm develop a C# window form application in VS2008. when run the exe file in PC. It come out message box.(object reference not set to an instance of an object).

When I change the connection string to normal style like

string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source= \\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";

is ok.

but when I'm use

ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
                string name = conSettings.ProviderName;
                string providerName = conSettings.ProviderName;
                string ConnectionString = conSettings.ConnectionString;

it come out the message box.

Here is code for my login form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public partial class Login : Form
    {
        
        public Login()
        {
            InitializeComponent();
            if (PropNearest != string.Empty)
            {
                this.txtUserName.Text = PropNearest;
            }
        }

        public string PropNearest
        {
            get
            {
                return this.txtUserName.Text;
            }
            set
            {
                this.txtUserName.Text = value;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            

            try
            {

                OleDbConnection conAuthor;
                
                ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
                string name = conSettings.ProviderName;
                string providerName = conSettings.ProviderName;
                string ConnectionString = conSettings.ConnectionString;
                //string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source= \\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";
                conAuthor = new OleDbConnection(ConnectionString);

                

                conAuthor.Open();

                OleDbCommand cmd = new OleDbCommand("SELECT UserName, Password, Roles FROM [User] WHERE UserName ='" + txtUserName.Text + "'and Password='" + txtPassword.Text + "'", conAuthor);
                


               

                OleDbDataReader dr = cmd.ExecuteReader();

                string userText = txtUserName.Text;
                string passText = txtPassword.Text;

                while (dr.Read())
                {

                    if ((dr["UserName"].ToString() == userText) && (dr["Password"].ToString() == passText))
                    {

                        if (dr["Roles"].ToString() == "Admin")
                        {

                            this.Hide();
                            MDIParent1 myForm = new MDIParent1();
                            
                           
                            myForm.toolStripStatusLabel2.Text = txtUserName.Text;
                            myForm.ShowDialog();
                        }
                        else
                        {

                            this.Hide();
                            MDIParent1 myForm = new MDIParent1();
                            
                            myForm.toolStripStatusLabel2.Text = txtUserName.Text;
                            myForm.ShowDialog();
                            
                            
                        }

                    }
                    else
                    {
                        MessageBox.Show("Login Fail!! Try again");


                    }
                }
                dr.Close();
                conAuthor.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
           

        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            
            
        }

        private void Login_Load(object sender, EventArgs e)
        {
            
            this.txtPassword.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtPassword_KeyUp);
            this.txtUserName.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtPassword_KeyUp);
        }



        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                btnLogin.PerformClick();
            }
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {

            /*OleDbCommand sCommand;
            OleDbDataAdapter sAdapter;
            OleDbCommandBuilder sBuilder;
            DataSet sDs;
            DataTable sTable;

            ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
            string name = conSettings.ProviderName;
            string providerName = conSettings.ProviderName;
            string ConnectionString = conSettings.ConnectionString;

            string sql = "SELECT DISTINCT Roles FROM [User] WHERE UserName='" + txtUserName.Text + "'";


            OleDbConnection connection = new OleDbConnection(ConnectionString);
            connection.Open();
            sCommand = new OleDbCommand(sql, connection);
            sAdapter = new OleDbDataAdapter(sCommand);
            sBuilder = new OleDbCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "User");
            sTable = sDs.Tables["User"];
            connection.Close();
            cboRoles.DataSource = sDs.Tables["User"];
            cboRoles.DisplayMember = "Roles";
            connection.Close();*/

        }

        private void cboRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        
        

       
    }
}

here is code in App.config for connection

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="MyDBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;\\Wdsharespace\Wansern_System\Wansern_Foam\Instyle Sofa\Instyle_Sofa.mdb&quot;" providerName="System.Data.OleDb" />
    <add name="WindowsFormsApplication1.Properties.Settings.Instyle_SofaConnectionString1" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;\\Wdsharespace\Wansern_System\Wansern_Foam\Instyle Sofa\Instyle_Sofa.mdb&quot;" providerName="System.Data.OleDb" />
  </connectionStrings>
</configuration>

object reference not set to an instance of an object

I'm develop a C# window form application in VS2008. When I'm run the exe file in my pc, it work. But when run the exe file in other PC. It come out message box.(object reference not set to an instance of an object).

When I change the connection string to normal style like

string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source= \\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";

is ok.

but when I'm use

ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
                string name = conSettings.ProviderName;
                string providerName = conSettings.ProviderName;
                string ConnectionString = conSettings.ConnectionString;

it come out the message box.

Here is code for my login form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public partial class Login : Form
    {
        
        public Login()
        {
            InitializeComponent();
            if (PropNearest != string.Empty)
            {
                this.txtUserName.Text = PropNearest;
            }
        }

        public string PropNearest
        {
            get
            {
                return this.txtUserName.Text;
            }
            set
            {
                this.txtUserName.Text = value;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            

            try
            {

                OleDbConnection conAuthor;
                
                ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
                string name = conSettings.ProviderName;
                string providerName = conSettings.ProviderName;
                string ConnectionString = conSettings.ConnectionString;
                //string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;DATA Source= \\\\Wdsharespace\\Wansern_System\\Wansern_Foam\\Instyle Sofa\\Instyle_Sofa.mdb";
                conAuthor = new OleDbConnection(ConnectionString);

                

                conAuthor.Open();

                OleDbCommand cmd = new OleDbCommand("SELECT UserName, Password, Roles FROM [User] WHERE UserName ='" + txtUserName.Text + "'and Password='" + txtPassword.Text + "'", conAuthor);
                


               

                OleDbDataReader dr = cmd.ExecuteReader();

                string userText = txtUserName.Text;
                string passText = txtPassword.Text;

                while (dr.Read())
                {

                    if ((dr["UserName"].ToString() == userText) && (dr["Password"].ToString() == passText))
                    {

                        if (dr["Roles"].ToString() == "Admin")
                        {

                            this.Hide();
                            MDIParent1 myForm = new MDIParent1();
                            
                           
                            myForm.toolStripStatusLabel2.Text = txtUserName.Text;
                            myForm.ShowDialog();
                        }
                        else
                        {

                            this.Hide();
                            MDIParent1 myForm = new MDIParent1();
                            
                            myForm.toolStripStatusLabel2.Text = txtUserName.Text;
                            myForm.ShowDialog();
                            
                            
                        }

                    }
                    else
                    {
                        MessageBox.Show("Login Fail!! Try again");


                    }
                }
                dr.Close();
                conAuthor.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
           

        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            
            
        }

        private void Login_Load(object sender, EventArgs e)
        {
            
            this.txtPassword.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtPassword_KeyUp);
            this.txtUserName.KeyUp += new System.Windows.Forms.KeyEventHandler(this.txtPassword_KeyUp);
        }



        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                btnLogin.PerformClick();
            }
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {

            /*OleDbCommand sCommand;
            OleDbDataAdapter sAdapter;
            OleDbCommandBuilder sBuilder;
            DataSet sDs;
            DataTable sTable;

            ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
            string name = conSettings.ProviderName;
            string providerName = conSettings.ProviderName;
            string ConnectionString = conSettings.ConnectionString;

            string sql = "SELECT DISTINCT Roles FROM [User] WHERE UserName='" + txtUserName.Text + "'";


            OleDbConnection connection = new OleDbConnection(ConnectionString);
            connection.Open();
            sCommand = new OleDbCommand(sql, connection);
            sAdapter = new OleDbDataAdapter(sCommand);
            sBuilder = new OleDbCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "User");
            sTable = sDs.Tables["User"];
            connection.Close();
            cboRoles.DataSource = sDs.Tables["User"];
            cboRoles.DisplayMember = "Roles";
            connection.Close();*/

        }

        private void cboRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        
        

       
    }
}

here is code in App.config for connection

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="MyDBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;\\Wdsharespace\Wansern_System\Wansern_Foam\Instyle Sofa\Instyle_Sofa.mdb&quot;" providerName="System.Data.OleDb" />
    <add name="WindowsFormsApplication1.Properties.Settings.Instyle_SofaConnectionString1" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot;\\Wdsharespace\Wansern_System\Wansern_Foam\Instyle Sofa\Instyle_Sofa.mdb&quot;" providerName="System.Data.OleDb" />
  </connectionStrings>
</configuration>

attach pic

Get rid of the '&quot;' in your connection strings. There are two in each.

Get rid of the '&quot;' in your connection strings. There are two in each.

you mean is delete both &quot;

Yes, the file should read

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="MyDBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Wdsharespace\Wansern_System\Wansern_Foam\Instyle Sofa\Instyle_Sofa.mdb" providerName="System.Data.OleDb" />
    <add name="WindowsFormsApplication1.Properties.Settings.Instyle_SofaConnectionString1" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Wdsharespace\Wansern_System\Wansern_Foam\Instyle Sofa\Instyle_Sofa.mdb" providerName="System.Data.OleDb" />
  </connectionStrings>
</configuration>

Yes, the file should read

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="MyDBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Wdsharespace\Wansern_System\Wansern_Foam\Instyle Sofa\Instyle_Sofa.mdb" providerName="System.Data.OleDb" />
    <add name="WindowsFormsApplication1.Properties.Settings.Instyle_SofaConnectionString1" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\Wdsharespace\Wansern_System\Wansern_Foam\Instyle Sofa\Instyle_Sofa.mdb" providerName="System.Data.OleDb" />
  </connectionStrings>
</configuration>

also same. It also pop out message box. I found that when I copy another together with exe, it work. the file is contain app.config. Any idea to solve the problem.
attach pic.

Do you mean that you were not copying the app.config with the executable? And that when you do it does work?

yes. when i copy app.config with exe, it work. when exe alone, it not work

That's how app.config files work. You need to copy them with the exe.

You can encrypt the data and the user won't be able to read it. Take a look at this and this

You can encrypt the data and the user won't be able to read it. Take a look at this and this

Thank You

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.