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="\\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>