Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 2nd, 2011, 08:24 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default populate an array using LINQ and foreach

Hi:

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:

Code:
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:

Code:
 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:
Code:
 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

Code:
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:

Code:
for (i = 0; i < recordsReturned; i++)
         total += realDiffs[i];
My sense is that the foreach statement is incorrectly constructed.

As always,
Cliff
Reply With Quote
  #2 (permalink)  
Old August 3rd, 2011, 03:39 AM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,039
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

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?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (August 3rd, 2011)
  #3 (permalink)  
Old August 3rd, 2011, 11:58 AM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default Debugging LINQ value

Hi:

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:

Code:
  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
Cliff
Reply With Quote
  #4 (permalink)  
Old August 3rd, 2011, 12:14 PM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,039
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

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?


Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (August 3rd, 2011)
  #5 (permalink)  
Old August 3rd, 2011, 01:41 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default

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:
Code:
  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.
Reply With Quote
  #6 (permalink)  
Old August 3rd, 2011, 02:19 PM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,039
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (August 3rd, 2011)
  #7 (permalink)  
Old August 3rd, 2011, 02:22 PM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,039
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

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.

Cheers.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (August 4th, 2011)
  #8 (permalink)  
Old August 4th, 2011, 02:33 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2011
Posts: 32
Thanks: 22
Thanked 0 Times in 0 Posts
Default Copied All

Hi:

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.
Reply With Quote
  #9 (permalink)  
Old August 4th, 2011, 06:51 PM
Imar's Avatar
Wrox Author
Points: 71,804, Level: 100
Points: 71,804, Level: 100 Points: 71,804, Level: 100 Points: 71,804, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,039
Thanks: 80
Thanked 1,580 Times in 1,557 Posts
Default

"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.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
esherr01 (August 4th, 2011)
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
foreach tedr BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 1 January 27th, 2010 06:07 PM
foreach confusion stokerfed C# 2 June 13th, 2007 03:30 PM
Populate a popup from an array bleutiger Javascript How-To 1 August 13th, 2005 05:31 AM
Populate Array with SQL query string nicoleh Classic ASP Databases 3 July 6th, 2005 03:55 PM
for loops from within foreach richard.york Pro PHP 5 October 2nd, 2003 02:54 AM



All times are GMT -4. The time now is 06:57 PM.


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