Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I give only F_Name or only F_Name & L_Name, than I get following error:
Index was outside the bounds of the array.

My code is
C#
string[] names = Receiver_Id_TextBox1.Text.Split;
F_Name = names[0];
M_Name = names[1];
L_Name = names[2];
cmd.CommandText = "SELECT PROFILE_ID, FIRST_NAME,MIDDLE_NAME,LAST_NAME FROM     DSProfile.HDR_PROFILE WHERE FIRST_NAME='" + F_Name + "'OR MIDDLE_NAME='" + M_Name + "' OR LAST_NAME= '" + L_Name + "' ";
string id = Convert.ToString(cmd.ExecuteScalar());
I want to give input either F_Name, or F_Name & L_Name, or F_Name, M_Name & L_Name.

What should i do to remove exception?
Posted
Updated 26-Nov-12 1:03am
v3

Try following code:-
C#
string[] names = Receiver_Id_TextBox1.Text.Split;
int arraySize = names.Lenth;
switch(arraySize)
{
   Case 1: //assign F_name;
    Break;
   Case 2: //assign F name & Lname
    Break
   case 3: // assign all three
    Break;
}
 
Share this answer
 
v2
Comments
mona_12 26-Nov-12 7:07am    
Thanks... It works.
Hi,

Have you debugged and checked what value are you getting in the names array actually?
Looking at the code it seems that names array will be having the value of Receiver_Id_TextBox1.
Debug and see whether are you getting enough values to actually initialize all of your name variables.
 
Share this answer
 
Comments
mona_12 26-Nov-12 6:06am    
yes i have checked. it get proper value. but if i give only F_Name in textbox, den it give exception.
Anurag Sinha V 26-Nov-12 6:15am    
Hi,
Can you post your full code?
It will help me to understand what really are you trying to do.

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