Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 System.IndexOutOfRangeException

Author  Topic 

stuckne1
Starting Member

22 Posts

Posted - 2010-10-11 : 13:23:56
This line is giving me trouble, but I'm not sure why because it matches the field name in the database...

string chkFed = reader2["chkFedw4Form"].ToString();

USE [MyDB_Workstudy]
GO
/****** Object: Table [dbo].[PhysicalDocumentsCheckboxes] Script Date: 10/11/2010 11:38:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[PhysicalDocumentsCheckboxes](
[parkID] [int] NOT NULL,
[chkFedw4Form] [varchar](5) NOT NULL CONSTRAINT [DF_PhyiscalDocumentsCheckboxes_chkFedw4Form] DEFAULT ('FALSE'),
[chkMow4Form] [varchar](5) NOT NULL CONSTRAINT [DF_PhyiscalDocumentsCheckboxes_chkMow4Form] DEFAULT ('FALSE'),
[chkI9PlusSupport] [varchar](5) NOT NULL CONSTRAINT [DF_PhyiscalDocumentsCheckboxes_chkI9PlusSupport] DEFAULT ('FALSE'),
CONSTRAINT [PK_PhysicalDocumentsCheckboxes] PRIMARY KEY CLUSTERED
(
[parkID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

--------------------------------------C# code below....

SqlCommand cmdCheck = new SqlCommand("SELECT parkID FROM PhysicalDocumentsCheckboxes WHERE parkID= " + id, conn);
conn.Open();
reader2 = cmdCheck.ExecuteReader();
while (reader2.Read())
{
string chkFed = reader2["chkFedw4Form"].ToString();
if (reader2[1].Equals("TRUE"))
{
chkFedMo4Form.Checked = true;
}
if (reader2[2].Equals("TRUE"))
{
chkMissouriW4Form.Checked = true;
}
if (reader2[3].Equals("TRUE"))
{
chkSupport.Checked = true;
}
}
conn.Close();
conn.Dispose();

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-10-11 : 13:37:19
Not sure about the double quotes around chkFedw4Form.
I'm not a C#-coder.
But your select isn't retreiving chkFedw4Form - it is parkID ???

And maybe this the solution:
http://www.daniweb.com/forums/thread283355.html


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

stuckne1
Starting Member

22 Posts

Posted - 2010-10-11 : 13:45:49
quote:
Originally posted by webfred

Not sure about the double quotes around chkFedw4Form.
I'm not a C#-coder.
But your select isn't retreiving chkFedw4Form - it is parkID ???

And maybe this the solution:
http://www.daniweb.com/forums/thread283355.html


No, you're never too old to Yak'n'Roll if you're too young to die.



your right...Imma dumbass lol.

Problem resolved!

SqlCommand cmdCheck = new SqlCommand("SELECT * FROM PhysicalDocumentsCheckboxes WHERE parkID= " + id, conn);
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-10-11 : 13:47:15



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-10-11 : 13:48:56
best to explicitly name the fields you want rather than SELECT *
Go to Top of Page

stuckne1
Starting Member

22 Posts

Posted - 2010-10-11 : 13:54:30
quote:
Originally posted by russell

best to explicitly name the fields you want rather than SELECT *



SqlCommand cmdCheck = new SqlCommand("SELECT chkFedw4Form, chkMow4Form, chkI9PlusSupport FROM PhysicalDocumentsCheckboxes WHERE parkID= " + id, conn);

Okay, but why exactly?

I assume that it's because I don't always need all of the rows in a table so selecting only the needed rows is more efficient, but in this circumstance I need to return all the rows of the table...
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-10-11 : 14:02:23
In this case not thrilling but no traffic for parkID because it is not needed.
Another cause is that your select is still running ok even if someone is adding columns to the table.
...and so on...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

stuckne1
Starting Member

22 Posts

Posted - 2010-10-11 : 14:06:15
quote:
Originally posted by webfred

In this case not thrilling but no traffic for parkID because it is not needed.
Another cause is that your select is still running ok even if someone is adding columns to the table.
...and so on...


No, you're never too old to Yak'n'Roll if you're too young to die.



Oh, alright that makes sense...I'll follow this practice, thanks guys.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-10-11 : 14:16:45
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -