Home > Mobile >  Asp.net System.NullReferenceException when creating new MysqlConnection
Asp.net System.NullReferenceException when creating new MysqlConnection

Time:03-20

`I'm new to asp.net and when I tried to connect to MySQL database I got this error. error message

here is my code in web.config file

<configuration>

  <appSettings>
    
      <add key="HostPrefix" value="/root" />
      <add key="ServerIp" value="localhost" />
      <add key="MysqlConnectionstring" value="server=localhost;Database=world;port=3306;user=root;password=1234;SslMode=none" />
      <add key="UserImagePath" value="world\city\" />
      
  </appSettings>

here is my code for database connection

using Common;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Configuration;

namespace Repositories
{
    public class StudentRepository
       
    {
         private string ConnectionstringMaster = WebConfigurationManager.AppSettings["MysqlConnectionString"];

  private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();        
       
public studentsModel UserRegistration(string Id )
        { studentsModel stud1 = new studentsModel();     

try
             {
string Query = "select ID,Name,CountryCode,District from world.city where ID='"   Id   "'";
using (var connection = new MySqlConnection(ConnectionstringMaster))
                 
                 {
                      using (var command= new MySqlCommand(Query,connection))
                      {
                          connection.Open();
                          using (var sdr=command.ExecuteReader())
                          {
                              while (sdr.Read())
                              {
                                  
                                  stud1.FirstName = sdr["Name"].ToString();

                              }

                          }
                      }
                  }
                  return stud1;
                
                
            }
            catch (Exception e)
             {
                Logger.Info("inside Exception Repo Userregistration  " e.ToString());
              
                return stud1;
             } 
            

           
        }
    }
}

Here I'm trying to get data from database and assign those values to studentmodel stud1 . But when I try to create new MySQL connection with my database in localhost it gives exception like this .

ispasswordExpired :connection.ispasswordExpired threw an exception of type 'System.nullReferenceException'

ServerThread :connection.ServerThread threw an exception of type 'System.nullReferenceException'

ServerVersion : connection.ServerVersion threw an exception of type 'System.nullReferenceException'

CodePudding user response:

You first need to add the connectionStrings section in your web.config file and remove it from the appSettings section like this:

<configuration>

  <connectionStrings>
    <add name="MysqlConnectionstring" connectionString="server=localhost;Database=world;port=3306;user=root;password=1234;SslMode=none" />
  </connectionStrings>
  
  <appSettings>  
      <add key="HostPrefix" value="/root" />
      <add key="ServerIp" value="localhost" />
      <add key="" value="" />
      <add key="UserImagePath" value="world\city\" />   
  </appSettings>
 
</configuration>

And then you can access the connection string value by accessing the ConnectionString property in your code like this:

private string ConnectionstringMaster = ConfigurationManager.ConnectionStrings["MysqlConnectionstring"].ConnectionString;

CodePudding user response:

You need to add a connectionStrings to your Web.Config.

Web.config:

<configuration>
  <connectionStrings>
    <add name="MysqlConnectionstring" connectionString="Server=myServerName;Port=3306;Database=myDatabaseName;Uid=testUser;Pwd=myPassword;" />      
  </connectionStrings>
                     ...
</configuration>
  • Related