App.Config文件连接字符串但它已连接并且正在获取空值引用错误 [英] App.Config file connection string but its connected and am getting the null value reference error
问题描述
大家好,
WPF应用程序中的新功能。我尝试使用sql server 2008数据库连接asp.net。我正在使用App.Config连接字符串但它已连接并且我得到空值引用错误。我知道这个错误为什么这会在我的应用程序中出现数据库连接字符串问题在这里,我在App.Config文件中添加了我的连接字符串。谁能请帮忙。
配置文件代码: -
Hi All,
Am new in WPF application. I tried connect asp.net with sql server 2008 database. I am using App.Config connection string but its connected and am getting the null value reference error. I know this error why this came in my app the database connectionstring problem. Here, I below added the my connection string in App.Config file. Can anyone please help.
Config File Code:-
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionstrings>
<add ConnectionString="Data Source=xxxxxx;Initial Catalog=WPFTraining;Persist Security Info=True;User ID=xxxxxx;Password=xxxxxx;Pooling=False" providerName="System.Data.SqlClient" name="ConString"/>
</connectionstrings>
</configuration>
Code Behind: -
Code Behind:-
private void FillDataGrid()
{
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
string CmdString = string.Empty;
using (SqlConnection con = new SqlConnection(ConString))
{
CmdString = "SELECT emp_id, name, dept, salary FROM employee";
SqlCommand cmd = new SqlCommand(CmdString, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("employee");
sda.Fill(dt);
grdemployee.ItemsSource = dt.DefaultView;
}
}
推荐答案
你需要打开连接
之前
SqlCommand cmd = new SqlCommand(CmdString,con);
Put
con.Open();
然后尝试
You need to open the connection
before
SqlCommand cmd = new SqlCommand(CmdString, con);
Put
con.Open();
then try it
<configuration>
<connectionstrings>
<<big>add name="ConString"</big> ConnectionString="Data Source=xxxxxx;Initial Catalog=WPFTraining;Persist Security Info=True; providerName="System.Data.SqlClient" name="ConString"/>
</connectionstrings>
</configuration>
for windows authentication
for windows authentication
<configuration>
<connectionstrings>
<add ConnectionString="Data Source=xxxxxx;Initial Catalog=WPFTraining;User ID=xxxxxx;Password=xxxxxx/>
</connectionstrings>
</configuration>
for sql server authentication
..这个
for sql server authentication
..try this
尝试直接提供连接。
这篇关于App.Config文件连接字符串但它已连接并且正在获取空值引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!