NullReferenceException: Object reference not set to an instance of an object

Dears,
Greetings,
I am challenged NullReferenceException repetitively, please help.

=================

  • NullReferenceException: Object reference not set to an instance of an object.

    • syncfusionspreadsheet.Controllers.HomeController…cctor() in HomeController.cs

28. private static readonly string connetionString = ConfigurationManager.ConnectionStrings["RelidbContext"].ConnectionString;
  • Show raw exception details

  • TypeInitializationException: The type initializer for ‘syncfusionspreadsheet.Controllers.HomeController’ threw an exception.

    • syncfusionspreadsheet.Controllers.HomeController.GetDataFromDB() in HomeController.cs

44. // private List<Order> GetDataFromDB()
45. private static List<Tablez> GetDataFromDB()
46. {
47. // List<Order> orders = new List<Order>();
48. List<Tablez> Tables = new List<Tablez>();
50. SqlConnection sqlCon = new SqlConnection(connetionString);
51. SqlCommand sqlComm = new SqlCommand("SELECT * FROM [dbo].[Table]", sqlCon);
52. try
53. {
54. sqlCon.Open();
55. SqlDataReader sqlDR = sqlComm.ExecuteReader();
56. while (sqlDR.Read())
  • syncfusionspreadsheet.Controllers.HomeController.Index() in HomeController.cs

41. @ViewBag.Datasource = GetDataFromDB();
  • lambda_method1(Closure , object , object )

===================

the code is here:
private static readonly string connetionString = ConfigurationManager.ConnectionStrings[“RelidbContext”].ConnectionString;
public ActionResult Index()
{
@ViewBag.Datasource = GetDataFromDB();
return View();
}

   // private List<Order> GetDataFromDB()
    private static List<Tablez> GetDataFromDB()
    {
       // List<Order> orders = new List<Order>();
        List<Tablez> Tables = new List<Tablez>();
        SqlConnection sqlCon = new SqlConnection(connetionString);
        SqlCommand sqlComm = new SqlCommand("SELECT * FROM [dbo].[Table]", sqlCon);
        try
        {
            sqlCon.Open();
            SqlDataReader sqlDR = sqlComm.ExecuteReader();
            while (sqlDR.Read())
            {
                /*Order order = new Order();
                order.OrderID = (int)sqlDR.GetValue(0);
                order.ItemName = sqlDR.GetValue(1).ToString();
                order.Quantity = sqlDR.GetValue(2).ToString();
                order.Price = sqlDR.GetValue(3).ToString();
                order.Amount = sqlDR.GetValue(4).ToString();
                orders.Add(order);*/


                Tablez Table = new Tablez();
                Table.OrderId = (int)sqlDR.GetValue(0);
                Table.ItemName = sqlDR.GetValue(1).ToString();
                Table.Quantity = sqlDR.GetValue(2).ToString();
                Table.Price = sqlDR.GetValue(3).ToString();
                Table.Amount = sqlDR.GetValue(4).ToString();
                Tables.Add(Table);
            }
        }
        catch (Exception)
        { 
            throw;
        }
        sqlCon.Close();
       // return orders;
        return Tables;
    }

Your post doesn’t show us where the exception is occurring. So I’m guessing a bit here.

I see nothing obviously “wrong” with the sequence of code, so you have to check the state of the variables. I.e. check that connetionstring is valid; check that sqlCon is valid, check that sqlComm is valid.

You’re using a syncfusion library, you might want to post your question in their forums on syncfusion.com