Question

I enter a name in textbox eg. "John" and save it in the database which it does, then i again enter "John" which it does not save. But if i enter "john", it saves this again... I need it to not save the john and vice-versa.

ok so this is the textbox which is quiet normal:

 <asp:TextBox ID="TextBox2" runat="server" Width="80%" BorderColor="Black" 
                    MaxLength="127"></asp:TextBox>

and here is the code:

int k= 0
SqlConnection dataConnection = new SqlConnection();
            dataConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlCommand dataCommand =
                    new SqlCommand("select Name from Groups", dataConnection);


            ArrayList names = new ArrayList();
            dataConnection.Open();
            SqlDataReader rdr = dataCommand.ExecuteReader();
            while (rdr.Read())
            {
                names.Add(rdr.GetString(0));
            }
            dataCommand.Dispose();
            dataConnection.Dispose();

            //to check if name already used..
            for (int i = 0; i < names.count; i++)
            {
                if (Name != (string)names[i])
                {
                    k = 1;

                }
                else
                {
                    Label1.Text = "Name is already present";
                    Label1.Visible = true;
                    k = 0;
                    break;
                }
            }
            if (k == 1)
            {
                Insertname();
            }
Was it helpful?

Solution

if (Name != (string)names[i])
{
    ...
}

could become

if (!Name.Equals((string)names[i], StringComparison.InvariantCultureIgnoreCase))
{
    ...
}

OTHER TIPS

One way to do it is to change it to proper case each time before presenting it to the database.

To get it to proper case, you can try the ToTitleCase() method in System.Globalization.TextInfo.

Note that ToTitleCase() provides an arbitrary casing behavior, not a linguistically correct behavior. For example "War and Peace" should have a lower case a in English. This is especially problematic for cases such as O'Brien, which ToTitleCase will output as O'brien.

In the end, it might be easier just to let the database save the thing.

Time for a cold shower.

First, if you want to check if the name exists and insert it if it doesn't you must check on the server, under proper isolation and transaction control. What you have there, besides being horrendously inefficient by checking the name vs. an array on the client, is incorrect because it checks if the name did not existed (past tense). It does not consider concurrency and as a result two requests can attempt to insert the same name.

Second, if you need uniqueness in the database then you enforce it by table constraints. You want a case insensitive unique name, then you should add an UNIQUE constraint to your table on the Name column and make sure the column uses a case insensitive collation.

Why not just force to uppercase (or lower case for that matter) in the comparison logic:

if (Name.ToUpper() != (string)names[i].ToUpper)

HTH

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top