Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I have a problem in my code.pls can anyone help me...

Problem: I have two textboxes one textbox should accept phrases(words) and another one should accept number(1-9)...
if i have entered number in the second textbox.according to textbox number it should sort the phrases(words)in gridview..
validations are first textbox shouldnot accept the duplicate name or phrases.
I have written code like this but it is accepting duplicate names and displaying in gridview...actually it should not accept duplicate.....so please can any one help me out.........pls.....Thanks in Advance........

my code is like this:
C#
{
        string phrses = TextBox1.Text;
        string[] split = phrses.Split(',');
        int count = phrses.Length - phrses.Replace(",", "").Length;
        int i = Convert.ToInt32(TextBox2.Text);

        int k1 = 0; int a1 = 0; int b = 0;
        for (a1 = count; a1 > 0; a1--)
        {
            for (b = count; b > 0; b--)
            {
                if (a1 != b)
                {
                    if (split[a1] == split[b])
                    {
                        k1 = 1;
                    }
                }
            }
        }
        if (k1 != 0)
        {
            Response.Write("enter different phrases");
        }
        string[] splichar = new string[split.Length];
        int j = 0;
        foreach (string a in split)
        {
            splichar[j] = a.Substring(i - 1);
            j++;
        }
        Array.Sort(splichar);

        j = 0;
        foreach (string k in splichar)
        {
            foreach (string p in split)
            {
                if (p.Contains(k))
                {
                    splichar[j] = p;
                }
            }
            j++;
        }

pls help me.........

[EDIT]Code tags added - LOSMAC[/EDIT]
Posted
Updated 17-May-12 6:48am
v4
Comments
Clifford Nelson 17-May-12 12:47pm    
I am a little concerned about the following:

int i = Convert.ToInt32(TextBox2.Text);
that could cause an exception if the user can make changes to TextBox2. Use the int.TryParse(): http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/549690f2-4f5e-4872-8d2c-7dcf529e53e4
Maheshdotnet2010 18-May-12 4:48am    
i am getting error that Error 1 No overload for method 'TryParse' takes '1' arguments

It can be checked whether a word or phrase is repeated in the given group of words separated by , either using LINQ or Regex as shown below:

C#
string words1 = "abcd,defgh xyz, abcd, defgh, abcd,  abcd, ijkl";
string words2 = "abcd,defgh xyz,  ijkl, mnop, qrst, uvw, abc, uvws, defghxyz";
int max = words1.Split(',').Select (w => w.Trim())
                .GroupBy (w => w).Max (w => w.Count ());
Console.WriteLine (max);

bool result = Regex.IsMatch(words1,
                @"(?:^\s*(?<word>[^,]+)\s*,|,\s*(?<word>[^,]+)\s*,)"+
                @"(?=.*,\s*\k<word>\s*,|.*,\s*\k<word>\s*$)",
                RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
Console.WriteLine (result);

//Output for words1
//4
//True

//Output for words2
//1
//False

The regular expression can be tested here http://regexhero.net/tester/[^]
 
Share this answer
 
v2
Comments
Maheshdotnet2010 18-May-12 4:40am    
I dont understand sir.......pls help me...my problem is my first textbox shouldnot accept the duplicate name or phrase so what should i do sir......
pls help me sir......Thanks in advance
I am not sure I totally understand the code. This line seems strange:

int count = phrses.Length - phrses.Replace(",", ";").Length;


The reason is that the Replace should not change the length, so the count should always be zero. Did you debug this?
 
Share this answer
 
Comments
Maciej Los 17-May-12 13:09pm    
This part of code was totally unreadable. Please, see the first post (without changes) updatable post.
Maheshdotnet2010 18-May-12 4:38am    
That is wrongly printed actually it is :Phrses.length-phrses.Replace(",","").length;

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