Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I am working on a c# form application which sends sql queries to SQL Server 2008.
I have a function which is supposed to split my update string so I can add fields depending on which checkboxes the user ticks.

I keed getting an error in this function:

C#
private string DivideInTwo(string toBeDivided,string AddedString)
        {
            string[] delimitator = new string[] { "set" };
            string[]  string1=toBeDivided.Split(delimitator,StringSplitOptions.None);
            
            string1[0] = string1[0] + " set ";
/*this is the error line*/string stringFinal = string1[0] +AddedString+ string1[1];

            return stringFinal;
        }


I get the error on this line:

C#
string stringFinal = string1[0] +AddedString+ string1[1];


specifically at the string1[1] part.

also using a for statement and an index didn't help at all.

Does anyone know what is wrong here?

Thank you in advance,

CrinaT.
Posted
Updated 19-Jul-12 15:35pm
v3
Comments
[no name] 19-Jul-12 21:30pm    
What is the value of toBeDivided?
CrinaT 19-Jul-12 21:48pm    
string toBeDivided = "update Evenimente set where Evenimente.id_E=(select id_E from Evenimente where nume_E like @numeEvenimentUniversal)";
[no name] 19-Jul-12 21:51pm    
Your code with that input string works perfectly fine for me.
CrinaT 19-Jul-12 22:06pm    
Well it worked fine for me at first,but by the time I got to the end of my five update statements with all their attributes it was giving me the index out of bounds error

I know what's wrong. It's exactly what the error says. When you call split, you are not guarenteed to have any specific number of elements, and in this case, your delimitator (sic) is not found in the string, so there's only one element, and your code blows up because it doesn't check it's assumptions.
 
Share this answer
 
Comments
CrinaT 19-Jul-12 21:52pm    
Do you know how I can fix this?

I tried handling it like a normal array:

string[] string1 = null;
for (int x = 0; x < 2; x++)
{
string1[x] = toBeDivided.Split(delimitator, StringSplitOptions.None);
}


and got Error 1 Cannot implicitly convert type 'string[]' to 'string'
Christian Graus 19-Jul-12 21:55pm    
It seems to me that you're wildly guessing, instead of programming. That's what your attempt here, is. It makes zero sense at all, and could never do anything useful.

Yes, you do what I told you to do. Before you try to access an index above 0, you check the Count property of your array to see if the index exists.
CrinaT 19-Jul-12 22:02pm    
I'm actually trying,but thanks for the irony.

You didn't tell me to do anything,you just told me what was wrong(which is kind of obvious,I'm looking for a solution)

Well I want it to exist even if he can see it or not.
I always have 2 strings in there,nothing more.
So mainly,do you know how I can always make it accept 2 elements?
Christian Graus 19-Jul-12 22:07pm    
You can't change how C# works. If you call split, then you will get an array, and the number of elements you get, depends on how often your delimiter is in the string. So, as soon as you use an index other than 0, you need to first check if it exists. I will keep telling you the same thing because what I am telling you is programming 101, and there are NO other options for you here.
check length before access each item.

C#
string stringFinal = string.Empty;
string[] delimitator = new string[] { "set" };
string[] strings = toBeDivided.Split(delimitator, StringSplitOptions.None);
if (strings.Length > 1)
{
    stringFinal = string.Format("{0} set {1}{2}", strings[0], AddedString, strings[1]);
}

return stringFinal;
 
Share this answer
 
v2
Comments
CrinaT 23-Jul-12 13:41pm    
Thank you so much,it helped me a lot!:)

One little comment on the code you wrote:
it's strings not string1 in the context:
stringFinal = string.Format("{0} set {1}{2}", string1[0], AddedString, string1[1]);
so what worked for me was :
stringFinal = string.Format("{0} set {1}{2}", strings[0], AddedString, strings[1]);

Thank you again>:D<
DamithSL 23-Jul-12 14:12pm    
Glad it helped you :)
yes it should be "strings", updated my answer.

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