p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 4 General Discussion (http://p2p.wrox.com/forumdisplay.php?f=561)
-   -   populate an array using LINQ and foreach (http://p2p.wrox.com/showthread.php?t=84565)

esherr01 August 2nd, 2011 07:24 PM

populate an array using LINQ and foreach

I am trying to populate an array from a series of fields in a table that come from various records. I am confident that the data is there.

In a method I have the following LINQ statement:


var HDiffquery = from RoundOfGolf in db.RoundOfGolfs
                      where (RoundOfGolf.Customer_ID == customer &&
                      RoundOfGolf.Player_ID == player)
                      select RoundOfGolf.HDiff;

The idea is that I will have "several" of these HDiffs from my table, but how many? Earlier in the code I have:


int recordsReturned = (from RoundOfGolf in db.RoundOfGolfs
                    where (RoundOfGolf.Customer_ID == customer && RoundOfGolf.Player_ID == player)
                    select RoundOfGolf).Count();

Which should give me the number of matching records,so now I do the following:

string[] diffs = new string[recordsReturned]; //create string array
to determine the array size needed and now I want to take ALL the mathcing HDiff field values and after converting to a string value assign to the array of correct size


int i = 0;
foreach (var result in HDiffquery)
        diffs[i] = result.ToString();
      }//end foreach

which is where I think I'm getting myself in trouble by using a foreach statement to theoretically populate the array with all of the HDiffs. I copy the string array into a matching double array as I need to do some mathematics on the array (adding) and that's where I get the error"Index is outside the bounds of the array" when I attempt the following:


for (i = 0; i < recordsReturned; i++)
        total += realDiffs[i];

My sense is that the foreach statement is incorrectly constructed.

As always,

Imar August 3rd, 2011 02:39 AM

Hi Cliff,

A few things:

1. You're not increasing i inside the loop, so you keep assigning to the first element.

2. Why not simply call ToArray on your query to let the framework deal with all of this?


esherr01 August 3rd, 2011 10:58 AM

Debugging LINQ value

I have scaled back my efforts (to above) in an attempt to see if I am getting proper values.

What I am looking at is:


  int recordsReturned = (from RoundOfGolf in db.RoundOfGolfs
                    where (RoundOfGolf.Customer_ID == customer && RoundOfGolf.Player_ID == player)
                    select RoundOfGolf).Count();

          amount = recordsReturned.ToString();
          TextBox rounds = ((TextBox)DetailsView1.FindControl("TextBox7"));
          rounds.Text = amount;

Where I am trying to take the returned int value recordsReturned and merely put that value into a TextBox to see if the value I'm getting is what I would expect (this value is necessary early in the previous code posted).

My error is "Object reference not set to an instance of an object" ? amount has been declared prior.

Can you help. thanks

Imar August 3rd, 2011 11:14 AM

And which "Object reference [is] not set to an instance of an object"? I assume the TextBox, as I can't see other problems with the code.

Question: why are you using a TextBox in the first place for debugging? Why not set a breakpoint, hit F5 and look at recordsReturned while you step through the code?


esherr01 August 3rd, 2011 12:41 PM

I am not able to access my web host remotely (localhost) and I don’t have a version of the code that runs locally.

All my connection strings point to my remote host. So my testing is to look for obvious bugs within my Web Developer, FTP up and then run things and see how it goes.

When I run within Microsoft Visual Web Developer 2010 Express and set a breakpoint and press F5 and step through code, my DetailsView that comes up has several dropdownlist that are normally populated by accessing my remote database. Since under VWD I cannot remotely access my database, these dropdownlists remain unpopulated.

Upon pressing “Insert” my error is “Input string was not in correct format” and thus I never get to my breakpoint to see the value I am interested in if I attempt this under VWD.

Thus, my thought was to try and put the value in question into a Textbox under “live” conditions since that was the only way I could see to try and get a sense of the value in question.

My latest thought is:

  List<RoundOfGolf> roundsList = (from RoundOfGolf in db.RoundOfGolfs
                              where (RoundOfGolf.Customer_ID == customer && RoundOfGolf.Player_ID == player)
                              select RoundOfGolf).ToList();

      int recordsReturned = roundsList.Count();

…and then attempt to place the value of recordsReturned into something viewable within a page during the running of the app.

Imar August 3rd, 2011 01:19 PM

IMO, that's a pretty difficult and time-consuming way to create and debug web applications. Understanding how your code runs and where it breaks is invaluable in tracking issues like this. If I were you, I'd:

1. Create a backup of the remote database
2. Restore it against your local SQL Server Express version
3. Modify the connection strings to work with the local database
4. Develop and test locally, while keeping track of changes you make to the database.
5. When you're ready to deploy, update the database and then send over the ASP.NET and other files to the live site.

Just my 2 cents.

With regards to your "latest thought": that would work, but it won't change anything. I don't think the counter variable is null as you haven't declared it as a nullable type, leading to another error than the one you're getting now.....


Imar August 3rd, 2011 01:22 PM

Oh, and two more things:

1. You could add a TextBox directly to the page (and not in the DetailsView), ruling out that it can be null and cause the code to crash.

2. Use the ASP.NET Trace facility to write to the trace log and then view the magic Trace.axd file to see what happened. This should also work on your remote server.



esherr01 August 4th, 2011 01:33 PM

Copied All

1) I have copied my "Site" to another part of the C drive and called the folder LocalHost/Site.
2) Changed the connectionstrings in web.config to "SQLExpress..... (seems OK)"
3) Ctrl + F5 from login.aspx and login
4)Gives error, "keyword not supported, ....datasource"
5)So, in Management/Default.aspx.cs I put a breakpoint very early on. This file is called from login.aspx
6) Back to login.aspx and press F5
7) Add a watchpoint
8)As I step along F11
9) Early on in Default.aspx.cs is a call
GofDatabaseContext db = new GolfDatabaseContext();
10) Which in turn calls App_Code/GolfDatabase.dbml/GolfDatabase.designer.cs
11) Where it points early on to the first occurrance of:
public GolfDatabaseDataContext().............
12) The line above is:
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
13)So, LINQ is the problem?
14) So lets reinstall ADO.NET Entity Data Model.
15) When I click the Add button I get, "Could not resolve mscorlib......." error that mentions .NET framework potentially having a problem.
16) So I have also reinstalled from library EntityFramework and also reinstalled .NET framework 4.0.
17) After a reboot my error about not supporting keyword "datasource" remains.

So my copying of hosted site version in an attempt to have a LocalHost version to work on is not working.

Is the above any help in making additional changes to local working version.

Imar August 4th, 2011 05:51 PM

"keyword not supported, ....datasource" sounds like a problem in the connection string more than anything else (it's data source with a space). Take a look at www.connectionstrings.com for examples of connection strings. If you can't make it work, post your connection string here, and describe your SQL Server setup.

This sounds like a simple configuration issue, but I need to know a bit more in order to help you out.



All times are GMT -4. The time now is 01:10 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.