Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,
I have a grid view in which a template field with text boxes are present. My issue is when i write a string with repeated values like ab,ab,cf,hj like this then a message should show.
I have used a custom validator for it,but it is not working.
My code is
C#
protected void CustomValidator5_ServerValidate(object source, ServerValidateEventArgs args)
 {

     CustomValidator cv = (CustomValidator)source;
     GridViewRow gvr = (GridViewRow)cv.NamingContainer;
     CheckBox chk = (CheckBox)gvr.FindControl("CheckBox1");
     TextBox txt1 = (TextBox)gvr.FindControl("TextBox2");
     TextBox txt2 = (TextBox)gvr.FindControl("TextBox6");
     TextBox txt3 = (TextBox)gvr.FindControl("TextBox3");
     Label lab = (Label)gvr.FindControl("Label1");
     string serials = txt2.Text;
     string[] sl = serials.Split(',');
     if (chk.Checked == true)
     {
         if (txt1.Text != "")
         {
             using (SqlCommand comm = new SqlCommand("select [Serial_Number_Required] from Products where Product=@Product ", con))
             {
                 con.Open();
                 //string Role = (string)(Session["Role"]);
                 // 2. define parameters used in command object
                 SqlParameter para = null;
                 para = new SqlParameter();
                 para.ParameterName = "@Product";
                 para.Value = lab.Text;
                 comm.Parameters.Add(para);

                 //Pass @Pages parameter

                 SqlDataReader reade = comm.ExecuteReader();
                 while (reade.Read())
                 {
                     Session["Serial_Number_Required"] = Convert.ToString(reade["Serial_Number_Required"]);
                 }
                 con.Close();
                 //Button Add = (Button)PreviousPage.FindControl("Button2");
                 if (Session["Serial_Number_Required"].ToString() == "Y")
                 {
                     for (int i = 0; i < sl.Length; i++)
                     {
                         for (int j = i + 1; j < sl.Length; j++)
                         {
                             if (sl[i].ToString() == sl[j].ToString())
                             {
                                 args.IsValid = false;
                             }
                             else
                             {
                                 args.IsValid = true;
                             }
                         }
                     }

                 }
             }

         }

     }
 }

ASP.NET
<asp:CustomValidator ID="CustomValidator5" runat="server" ErrorMessage="Enter unique serialNo."
OnServerValidate="CustomValidator5_ServerValidate" ValidateEmptyText="true"
ControlToValidate="TextBox2" Display="Dynamic"
style="font-size: x-small; font-family: Verdana" ValidationGroup="txt"
Font-Bold="True" ForeColor="Red"></asp:CustomValidator>
Posted
Updated 25-Sep-14 23:18pm
v3

C#
for (int i = 0; i < sl.Length; i++)
                        {
                            for (int j = i + 1; j < sl.Length; j++)
                            {
                                if (sl[i].ToString() == sl[j].ToString())
                                {
                                    args.IsValid = false;
//you should break the loop here
break;
                                }
                                else
                                {
                                    args.IsValid = true;
                                }
                            }
//if already match found break from here as well
if(args.IsValid == false) break;

                        }
 
Share this answer
 
Comments
Sinisa Hajnal 26-Sep-14 6:19am    
Replace ToString with .Trim if "abc" and "abc " should be treated as same entry...
ToString is unnecessary since you know you're using string array.
Member 10578683 26-Sep-14 6:43am    
Thank U Very much
If you're using .net 3.5 and above:

C#
sl = (string[])sl.Distinct().ToArray(typeof(string));


This way you're only processing distinct values

If it is important to warn the user, then you create new Array and compare length of sl and this new array.

C#
String[] sl_distinct = (string[])sl.Distinct().ToArray(typeof(string));
if (sl.length != sl_distinct.length){
// your message
}
 
Share this answer
 
v4

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