I have a problem withthe connectionString I think.
I have this:
public List<Usuario> getUsuario()
{
List<Usuario> usuarios = new List<Usuario>();
string strConn = ConfigurationManager
.ConnectionStrings["BDLocal"]
.ConnectionString;
using (SqlConnection conn = new SqlConnection(strConn))
and in the web.config:
<connectionStrings>
<add
name="BDLocal"
connectionString="Data Source=DESKTOP-EJ\SQLEXPRESS;Initial
Catalog=Crud.Data;Persist Security Info=True;User
ID=user;Password=pass"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
But the message I get when I go to api/usuario is:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.
I know there must be something I'm doing wrong but I don´t know what it is.
Related
App.config file configuration 1
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyDBConnectionString" connectionString="Data Source=223.29.207.133;
Initial Catalog=MBV5DBLive;User ID=smsuser;Password=sms;IntegratedSecurity=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
App.config file configuration 2
<configuration>
<connectionStrings>
<add name="MyDBConnectionString" connectionString="Data Source=localhost;
Initial Catalog=University;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
C# code
try
{
String constring = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constring))
{
}
}
catch (Exception ex)
even if i debug when it comes to configuration manager code will jump to catch block and it will show "Object reference not set to an instance of an object."
What is the issue with above both configuration files?
Your connection string name seems like c_str not MyDBConnectionString.
And since you don't have any MyDBConnectionString in your web config, your ConfigurationManager.ConnectionStrings["MyDBConnectionString"] returns null and that's why you get NullReferenceException when you try to access ConnectionString property of a null reference.
If everything is okey other than that, this should work;
var constring = ConfigurationManager.ConnectionStrings["c_str"].ConnectionString;
using (var con = new SqlConnection(constring))
{
}
we are working on a project in which we need to show places on google map. For places, we are providing latitude and longitude from database. we are facing null reference exception error in the following place:
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
Integrated Security=True"].ConnectionString))
How to resolve this error please guide me.
Cause of Exception:-
When you say:
System.Configuration.ConfigurationManager.
ConnectionStrings["Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
Integrated Security=True"]
Since there is no connection string with name Data Source=KhUSHAL.., thus ConnectionStrings will return null and on that you are trying to access ConnectionString property which will result in Null reference exception. Read about this error here.
Basically you are mixing both, either do this:-
string CS ="Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;Integrated Security=True";
using (SqlConnection con = new SqlConnection(CS))
{
//Your code
}
Or fetch it from Web.Config(Preferred way):-
First define the connection in Web.Config:
<connectionStrings>
<add name="Test" connectionString="Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Then read it like this:-
using (SqlConnection con = new SqlConnection(System.Configuration
.ConfigurationManager.ConnectionStrings["Test"].ConnectionString))
{
//Your code
}
Do you have the ConnectionString in the Web.Config of the UI project?
Fix:
Copy that ConnectionString and Paste in your Web.Config
Your code,
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["Data Source=KHUSHALI\\SERVER;Initial Catalog=gis;
Integrated Security=True"].ConnectionString))
is Invalid, this is not the way to declare connection string and access them.
How can we declare Connection Strings and can Access them??
No1:>In a Page
string strConnectionString="server=localhost;database=myDb;uid=myUser;password=myPass;;
Integrated Security=True";
using (SqlConnection con = new SqlConnection(strConnectionString))
{
}
No2.>Web.Config you can declare then under configuration and appSeting
And Can Access Like:
using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings("myConnectionString")))
{
}
No3>Web.Config you can declare then under configuration and connectionStrings
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
And Can Access Like:
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString))
{
}
when i run this code this error comes..
object reference not set to an instance of an object.
AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConString))
{
SqlCommand cmd = new SqlCommand("SELECT NAME FROM CUSTOMER", con);
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
AutoCompleteStringCollection MyCollection = new AutoCompleteStringCollection();
while (reader.Read())
{
MyCollection.Add(reader.GetString(0));
}
txtcname.AutoCompleteCustomSource = MyCollection;
con.Close();
}
Make sure your connection string in web.config something like
<connectionStrings>
<add name="ConString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
Problem : i'm sure that you didn't declare the connection string in your config file under <ConnectionStrings> section.
OR
you might have declared it with some defferent Key name but not ConString.
Solution: you should declare the proper Connection String with Name ConString in your config file under <ConnectionStrings> section.
Make sure that your config file either App.Config or web.config has ConnectionString similar to following:
<connectionStrings>
<add name="ConString" connectionString="Data Source=ServerName;Intial Catalog=DatabaseName;UID=userID;Password=password;Integrated Security=True;" />
</connectionStrings>
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 9 years ago.
I got this error Object reference not set to an instance of an object. I can't find what is the problem. Thanks
protected void Check_Clicked(Object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["DbCar"].ConnectionString);
.....
}
<connectionStrings>
<add name="ConnectionString" connectionString="Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DbCar.mdf;Integrated
Security=True;User
Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Your connection string name ConnectionString not DbCar
new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["ConnectionString"].ConnectionString);
The name of the connectionstring is ConnectionString rather than DbCar
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["ConnectionString"].ConnectionString);
ConnectionStrings["DbCar"]
You do not have a connection string by that name. You need to rename your connection string from ConnectionString to DbCar or change the name in the code to ConnectionString.
Problem : You are refering to invalid connectionstring name. there is no ConnectionString in your configuration file with name DbCar.
Solution : you need to refer to the valid ConnectionString Name from the configuration file.
Solution 1: either change the name of DbCar to ConnectionString from the code.
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["ConnectionString"].ConnectionString);
Solution 2: or change the name of the connectionstring in config file from ConnectionString to DbCar.
<connectionStrings>
<add name="DbCar" connectionString="Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DbCar.mdf;Integrated
Security=True;User
Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Compiler Error Message: CS0118: 'System.Configuration.ConfigurationManager.AppSettings' is a 'property' but is used like a 'method'
<add key="ObjConn" value="Provider=SQLOLEDB;Persist Security Info=True;User ID=OMembers;PWD=OMembers;Initial Catalog=Db;Data Source=""/>
strconnection = System.Configuration.ConfigurationManager.AppSettings("ObjConn");
sqlcon = new SqlConnection(strconnection);
in C#, do this:
strconnection = System.Configuration.ConfigurationManager.AppSettings["ObjConn"];
sqlcon = new SqlConnection(strconnection);
I believe you're working in C#? You need to access it using index operator:
strconnection = System.Configuration.ConfigurationManager.AppSettings["ObjConn"];
Better to define connection strings in the connection strings section as so:
<connectionStrings>
<add
name="ObjConn"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
And instantiate you SqlConnection like this:
strconnection = System.Configuration.ConfigurationManager.ConnectionStrings["ObjConn"].ConnectionString;
sqlcon = new SqlConnection(strconnection);