Bhavesh R

Bhavesh R

  • 2k
  • 11
  • 133

Am getting error while connecting by db with visual studio 2019

May 1 2021 7:46 AM
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace aspsnippets.Models
{
public class DBconnection
{
public List<asppiechart> DBconnect()
{
List<asppiechart> chartData = new List<asppiechart>();
string connectionString = ConfigurationManager.ConnectionStrings["conmydb"].ToString();
SqlConnection sqlConnection = new SqlConnection(connectionString);
string cmd1 = "select * from piechart";
SqlCommand sqlCommand = new SqlCommand(cmd1, sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
while(sqlDataReader.Read())
{
chartData.Add(new asppiechart
{
Month = sqlDataReader["Month"].ToString(),
Values = sqlDataReader["Values"].ToString()
});
}
return chartData;
}
}
}
 
 
AM GETTING THIS ERROR:
 
System.NullReferenceException: 'Object reference not set to an instance of an object.'
System.Configuration.ConnectionStringSettingsCollection.this[string].get returned null.
 
 
 
 

Answers (11)