Click here to Skip to main content
12,449,919 members (45,439 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET SQL
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
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 25-Nov-12 23:49pm
mona_12384
Updated 26-Nov-12 1:03am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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.
anurag3487 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Try following code:-
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;
}
  Permalink  
v2
Comments
mona_12 26-Nov-12 7:07am
   
Thanks... It works.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100