你调用的对象是空的. -App.config [英] Object reference not set to an instance of an object. - App.config
问题描述
我收到错误,并且在本地窗口中看到conSettings和connectionString值均为null.我正确地说ConfigurationManager为null,我需要创建一个新对象.也许我正在使用Access,也许我错过了App.config文件中的某些内容.请有人能帮助我解决这个问题.预先感谢.
I receiving the error and in the local window I am seeing for both conSettings and connectionString value of null. I am right to say ConfigurationManager is null and I need to create a new object. Maybe I am using Access and perhaps I have missed something in App.config file. Can someone help me on how to solve this problem, please. Thanks in advance.
App.config文件...
App.config file...
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="MyDBConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\...\Database1.mdb"/>
</appSettings>
</configuration>
Form.cs文件...
Form.cs file...
private void btnShow_Click(object sender, EventArgs e)
{
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
string connectionString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString; // error points here
try
{
con = new OleDbConnection(connectionString);
con.Open();
cmd = new OleDbCommand("SELECT * FROM Table1", con);
objReader = cmd.ExecuteReader();
while (objReader.Read())
{
txtID.Text = ds.Tables[0].Rows[rno][0].ToString();
CBAgeGroup.Text = ds.Tables[0].Rows[rno][1].ToString();
CBGender.Text = ds.Tables[0].Rows[rno][2].ToString();
CBCrimOffen.Text = ds.Tables[0].Rows[rno][3].ToString();
if (ds.Tables[0].Rows[rno][4] != System.DBNull.Value)
{
photo_aray = (byte[])ds.Tables[0].Rows[rno][4];
MemoryStream ms = new MemoryStream(photo_aray);
pictureBox1.Image = Image.FromStream(ms);
}
txtCV.Text = ds.Tables[0].Rows[rno][5].ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
已建议我使用App.config.
I have been advised to use App.config.
VS 2010 C# MS Access 2003
VS 2010 C# MS Access 2003
更新1 我的App.config现在看起来像这样...
UPDATE 1 My App.config now looks like this...
<configuration>
<ConnectionString>
<add key="MyDBConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Raj\Education\C_Sharp\Test1\Database1.mdb"/>
</ConnectionString>
我现在收到以下错误:配置系统初始化失败".我现在正在Google上查看它.
I am now receiving error of..."Configuration system failed to initialize". I am looking at it now on Google.
更新2 尝试过...
<configuration>
<connectionStrings>
<add name="MyDBConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\...\Database1.mdb"/>
</connectionStrings>
</configuration>
再次收到谷歌搜索对象引用未设置为对象实例的错误"
Receiving error of "Object reference not set to an instance of an object" Googling again
更新3
<configuration>
<connectionStrings>
<clear />
<add name="MyDBConnectionString"
providerName="System.Data.OleDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Source=\Database1.mdb" />
</connectionStrings>
在更新3中,我收到相同的错误.我已经包括了添加参考系统.配置,我已经使用System.Configuration进行了引用;
With the update 3 I am receiving error the same error. I have included the Add reference System. Configuration and I have referenced using System.Configuration;
结论
也许VS 2010和Access 2003之间可能存在技术问题.这次我将不使用App.config.我知道SQL Server不会有问题.因此,我将保留该内容.感谢Damith和Clint的宝贵时间.
Perhaps it maybe there is a technical gitch between VS 2010 and Access 2003. I shall not use App.config this time round. I know there will be no problem with SQL Server. So I will leave it that. Thanks Damith and Clint for your time.
推荐答案
您需要阅读以下AppSettings键,
you need to read AppSettings key as below ,
string connectionString =
ConfigurationSettings.AppSettings["MyDBConnectionString"];
您仍然收到空值,请尝试以下步骤
still you receive empty value, try below steps
- 在解决方案资源管理器中选择App.Config文件
- 在属性窗口中,选择复制到输出目录以进行复制" 总是.
- 现在,构建应用程序,然后重试.
- Select the App.Config file in the solution explorer
- In the property window select Copy to Output Directory to Copy Always.
- Now Build the application and try again.
要像下面那样访问,您需要在应用程序配置中添加connectionStrings部分
to access like below you need to add connectionStrings section in app config
string connectionString =
ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString; // error points here
示例应用配置
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="MyDBConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=E:\...\Database1.mdb"/>
</connectionStrings>
</configuration>
这篇关于你调用的对象是空的. -App.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!