Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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
<?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:-

C#
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;
            }
        }
Posted
Updated 13-Nov-13 0:30am
v3
Comments
thatraja 13-Nov-13 5:03am    
Include the code in your question
IsaiSelvan 13-Nov-13 5:10am    
this is my code behind code:

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;
}
}
thatraja 13-Nov-13 5:37am    
In which line the error occurs?
Jamie Head 10-Aug-20 16:19pm    
I am getting the same error here -> https://www.codeproject.com/Questions/5276170/Sqlite-Csharp-why-am-I-getting-a-null-connection I dont seem to know what is causing it. Maybe its something to do with it being a console app. I have tried manually giving it a connectionString and I still get the same error.

You need to open the connection
before
SqlCommand cmd = new SqlCommand(CmdString, con);
Put
con.Open();

then try it
 
Share this answer
 
Comments
IsaiSelvan 13-Nov-13 7:39am    
I am getting same error message deepgalley
[no name] 13-Nov-13 8:25am    
just debug the code and let me know on exactly which line getting error.
XML
<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


XML
<configuration>
  <connectionstrings>
    <add ConnectionString="Data Source=xxxxxx;Initial Catalog=WPFTraining;User ID=xxxxxx;Password=xxxxxx/>
  </connectionstrings>
</configuration>



for sql server authentication


..try this
 
Share this answer
 
v4
Try to give the connection directly.
 
Share this answer
 

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