yongchang
yongchang

Reputation: 522

SQL Server query: Uncaught TypeError: content is null

What I am trying to do, is query from my database and populate the columns inside my Index.cshtml.

Below is my code:

This is from my HomeController:

SqlConnection con = new SqlConnection("Data Source=LAPTOP;Initial Catalog=SchedulerEvent;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");

public JsonResult GetEvents()
{
    con.Open();

    SqlCommand command = new SqlCommand("SELECT * FROM Event", con);
    command.ExecuteNonQuery();
    var events = command;
    return new JsonResult { Data = events, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

This is what my database looks like:

enter image description here

Further inspection shows me the area where it is null

enter image description here

I console log on my frontend and the image below is what it print:

enter image description here

This should not be the case since my query is SELECT * FROM Event but it is not fetching the content of my table.

But the thing is, as you have seen from my database screenshot, the column Subject is not empty or null.

Any advice is appreciated

Upvotes: 0

Views: 106

Answers (1)

Walnut
Walnut

Reputation: 318

you are returning the "command" of type "SqlCommand", this doesnt contain the result/data returned by the query.

you need to implement ExecuteReader()

using (SqlDataReader oReader = command.ExecuteReader())
{
    while (oReader.Read())
    {
        // prepare your object to return here.
        Console.WriteLine(oReader["EventId"].ToString());
        Console.WriteLine(oReader["Subject"].ToString());
    }
}

Upvotes: 1

Related Questions