0

I am new to c# and web services... when i debug the login coding it run on the browser but when enter the login button it shows error Object reference not set to an instance of an object with the source error as below:

{ SqlConnection DBConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString); try { 

is there any syntax that missing or error connection with database?

3
  • does a connection with name "ASPNETDBConnectionString" exist in your app/web.Config ? Commented Sep 3, 2012 at 10:06
  • do u have checked the connectionstring in web.config file? Commented Sep 3, 2012 at 10:06
  • Please show as the complete stacktrace of the error/exception you're getting. Also consider How to Ask. Commented Sep 3, 2012 at 10:06

3 Answers 3

1

You don't have a connection string with ASPNETDBConnectionString name, that is why you are getting this error. Check it against null before using it.

if(System.Configuration.ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"] != null) SqlConnection DBConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString); 
Sign up to request clarification or add additional context in comments.

1 Comment

It helped me,thank you! i've try this and it said that ASPNETDBConnectionString have been added.
0

1 Add reference on System.Configuration

2 Verify that ASPNETDBConnectionString string connection exist in your App.Config, or your Web.Config

Nota : you must have this section ASPNETDBConnectionString in your file of config

<connectionStrings> <add name="ASPNETDBConnectionString" connectionString="Data Source=serverName;Initial Catalog=...;Persist Security Info=True;User ID=userName;Password=password" providerName="System.Data.SqlClient" /> </connectionStrings> 

1 Comment

i do like u said..when i debug it there was no error but then after i enter the login, it said that "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)". what's was that problem?
0
firstly add connection string to web config file as follows: ***<configuration> <system.web> <compilation debug="true" targetFramework="4.0"/> </system.web> <connectionStrings> <add name="key" connectionString="....." providerName="System.Data.SqlClient"/> </connectionStrings> </configuration>*** then add namespace to .cs page > using System.Configuration; then create a connection string on page as:- ***cn = new SqlConnection(); cn.ConnectionString = ConfigurationManager.ConnectionStrings["key"].ConnectionString;*** 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.