Can someone help me out with this?

So i’m trying to connect to an existing sql database and print to the screen a data dump of all 5 fields in a sql table in a sql express database using pure code technique but whenever I run the application I get the following error

  • An unhandled exception of type ‘System.TypeInitializationException’ occurred in mscorlib.dll

Additional information: The type initializer for ‘MyProject’ threw an exception.

This is my code,

class Program { private static SqlConnection sqlConnection = new 

SqlConnection(“DataSource(LocalDB)MSSQLDB;AttachDBFileName =C:DatabasesHomeInventorySystem_EIS.mdf;” + “Integrated Security=True”);

 private static void ShowTable() { try { // Sql Select Query string sql = "SELECT * FROM BEDROOM"; SqlCommand cmd = new SqlCommand(sql, sqlConnection); SqlDataReader dr; dr = cmd.ExecuteReader(); string strId = "Id"; string strItem = "Item"; string strDescription = "Description"; string strWhenBought = "WhenBought"; string strModel = "Model"; string strCost = "Cost"; WriteLine("{0} | {1} | {2} | {3} | {4} | {5} ", strId.PadRight(10), strItem.PadRight(10), strDescription.PadRight(10), strWhenBought.PadRight(21),strModel.PadRight(22), strCost.PadRight(22)); WriteLine("============================================================================="); while (dr.Read()) { //reading from the datareader WriteLine("{0} | {1} | {2} | {3} | {4} | {5} ", dr["Id"].ToString().PadRight(10), dr["ItemName"].ToString().PadRight(10), dr["WhenBought"].ToString().PadRight(10), dr["Model"].ToString().PadRight(22), dr["Cost"].ToString().PadRight(22)); } dr.Close(); WriteLine("=============================================================================="); } catch (SqlException ex) { // Display error WriteLine("Error: " + ex.ToString()); } } private static void OpenConnection() { try { // Open Connection sqlConnection.Open(); WriteLine("Connection Opened"); } catch (SqlException ex) { // Display error WriteLine("Error: " + ex.ToString()); } } static void Main(string[] args) { OpenConnection(); Console.WriteLine("n"); ShowTable(); Console.WriteLine("n"); } } 

\Any help on how to resolve this issue will be greatly appreciated! Apologies about any formatting issues, Please help me out on this!
Thanks!

submitted by /u/Less_North
[link] [comments]

Leave a Reply