I'm going to create a student information system based on a tutorial. As soon as a user wants to add a new student to the database the following exception occurs.
I tried to learn something about TypeInitializationException and I understand a little by its name.. but I can't get it totally. Besides, the exception is absent from the tutorial I'm following.. I'm new in this type of OOP programming and handling errors. I included my database connection and database access class below..

My DB_Connection class
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace studentInformationSystem
{
class DB_Connection
{
public static SqlConnection NewConnection;
public static string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
public static SqlConnection getConnection()
{
NewConnection = new SqlConnection(ConString);
return NewConnection;
}
}
}
My DB_Access class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace studentInformationSystem
{
class DB_Access
{
public SqlConnection conn;
public DB_Access()
{
conn = DB_Connection.getConnection();
}
public void AddStudent(string regNo, string fName, string lName, string phoneNumber)
{
if(conn.State.ToString() == "Closed")
{
conn.Open();
}
SqlCommand newCommand = conn.CreateCommand();
newCommand.Connection = conn; //why we use it..???
newCommand.CommandType = CommandType.Text;
newCommand.CommandText = "insert into student values('" + regNo + "','" + fName + "','" + lName + "','" + phoneNumber + "')";
newCommand.ExecuteNonQuery();
}
}
}
here is my connectionString
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<configSections>
<connectionStrings>
<add name="ConString" connectionString="Data Source=SADID-PC\SQLEXPRESS;Initial Catalog=Test1;Integrated Security=True"
providerName="System.Data.sqlClient" />
</connectionStrings>
</configSections>
</configuration>