Click here to Skip to main content
15,886,782 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
DataRow[] Typliste = dsret.Select(String.Format("Label = '{0}'", strFeldnamen));
           
           try
           {
               Datentyp = "";
               Datentyp = Typliste[0].ItemArray[1].ToString();
           }
           catch
           {
               return false;
           }


then its throwing System.IndexOutOfRangeException how to fix it.
Posted
Updated 18-Nov-14 9:31am
v3
Comments
PIEBALDconsult 18-Nov-14 9:53am    
Either the record wasn't found or it has only one item.
Look before you leap. Use the debugger.
johannesnestler 18-Nov-14 10:10am    
You speak german - ok, but "Index out of range" should be easy to understand. You are using 3 indices in this snippet, first is implicit in String.Format - this is ok, So problem could only be that no row was returned by your select or the row hasn't two cells (unlikely you got that wrong but you see there is a reason not to rely on index - what if datarow format changes...). Use the Debugger not a forum for such simple coding errors you will see every day.
[no name] 18-Nov-14 15:51pm    
and BTW:

Datentyp = ""; // This line is not really meaningful
Datentyp = Typliste[0].ItemArray[1].ToString();

Easy enough. Don't try and use an index that doesn't exist and you won't get the error.

Your code is ASSUMING that are more items in an array than actually are there.

Either the Select is not returning any rows or it's returning fewer columns than your code is assuming are in the results.
 
Share this answer
 
Store the number of elements into a variable and use those to access the list rather than hard coded values.
For example.
int numOfLabels = Typliste.Length();
and/or
if(numOfLabels > 0)
{
Datentyp = Typliste[0].ItemArray[1].ToString();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900