5

I am trying to set the mysql connection in web config file.

Here is the connection string code:

    <connectionStrings>
    <add name="connstring" 
         connectionString="DRIVER={MySQL ODBC= 3.51=        Driver};Database=marctest;Server=localhost;UID=root;PWD=1234;" 
         providerName="System.Data.SqlClient"/>
    </connectionStrings>

and i am accessing it in following step:

    MySqlConnection connmysql = new MySqlConnection(WebConfigurationManager.ConnectionStrings["connstring"].ConnectionString);

When I run my code it generates a null reference exception."Object reference not set to an instance of an object."

How can I resolve this issue?

1

3 Answers 3

9

If you are using the MySql .Net Connector then your configuration setting should look similar to the following

<add name="connstring" connectionString="server=localhost;User Id=root;Persist Security Info=True;database=marctest;password=PASSWORD" providerName="MySql.Data.MySqlClient"/>

Then your code should look like.

using (MySqlConnection dbConn = new MySqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ConnectionString)) 
{ 
    // Database work done here 
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am using a similar kind of code but the issue is still not being resolved!
"Similar" or the same, as the "connectionString" setting is very important, if it is incorrect the connection will not be initialized. Check the spelling of "connstring" is the same on the config file and the code.
4

I think you have MySql Connector/Net API. Change the ProviderName attribute and have a look at ConnectionStrings. It should be MySql.Data.MySqlClient and use System.Configuration.ConfigurationManager.ConnectionStrings collection.

2 Comments

This has been done already but still having the same error. Any other way to resolve this?
What web.config is used by you? Is it at root or sub-dir?
0

You should check the following first:

ConfigurationManager.ConnectionStrings["connstring"].ConnectionString

returns a string, you can write it to output with Response.Write or look at it using the debugger

if you have the string from the step above, check to see the class WebConfigurationManager that you are using is instantiated and is not null.

if you don't get string from the 1st step, you might have another web.config in your code folder which would be a virtual directory and could be overriding your web.config

Your connectionStrings section should be inside configuration of Web.config file and should look like following

<configuration>
  <connectionStrings>
    <add name="connstring" connectionString="..." providerName="..." />
  </connectionStrings>

  <appSettings>
  ...
  </appSettings>
</configuration>

try reading the connection string from a pure *.ASPX page and see if you can access it first.

create an ASPX file and paste following in it and call it from your browser.

<%@ Page Language="C#" %>
<% 
Response.Write(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString); 
%>

if still you cannot read the connection string, please edit your question and add full web.config and also class file that you are trying to create your connection for me to see what might go wrong

5 Comments

It does not return the string value itself (throws the same error). And I do not find any other web.config file in my Projects folder. Any other way this can be resolved??
I updated my answer, please let me know if you still can't see the connection string in your page
I'm suspecting you are creating your connection from a class other than System.Web.UI.Page so the object ConfigurationManager is not instantiated automatically
This would sound weired.. but I created a new project and added my code there and edited the webconfig file with the above mentioned suggestions and it worked.... is there any chance that webconfig file got corrupted or something??
It might be, I can't say anything until I see your web.config and class code, you can remove all other methods and leave using section, class name, inheritance and method that you are creating your connection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.