Wicked Sick
Wicked Sick

Reputation: 43

How to get row count in asp.net and transfer asp.net row count to javascript?

I'm trying to get the value of an asp.net variable to javascript. Firstly, i wanted to get the number of rows in my database using asp.net and entity framework. Here is the query:

getRowCountThmb = (from g in obj.thumbnailImages where g.instrumentItem.brand.name == stringInstrumentName && g.instrumentItem.model == stringInstrumentModel select g).Count();

And then, the retrieved row count will be passed on to the getRowCountThmb property which is like this(auto-implemented): public int getRowCountThmb { get; set; }. And after that, i will get the value of getRowCountThmb in javascript like this: var srcArray = "<%= getRowCountThmb %>";

The problem is, I have found out that the getRowCountThmb is not actually getting any row count in the database. The value is 0. Resulting to a NaN value in the javascript variable called scrArray. Kindly give advice or provide solutions for this.

Upvotes: 0

Views: 339

Answers (1)

The_Torst
The_Torst

Reputation: 401

This happens because you put getRowCountThmb in quotation marks when injecting it (whether its value is 0 or not does not matter), making srcArray a string. NaN stands for "Not a Number", which is correct in this case.

Essentially, what you end up having in your source code is:

var srcArray = "0"; // <-- This is not a number, but a string (NaN)

You can solve this by removing the quotes like so:

var srcArray = <%= getRowCountThmb %>;

Upvotes: 1

Related Questions