Asked by:
Repeat Data Should Not Store Again

Question
-
User-807418713 posted
Hello
As I Have One Table Name Is "Master_Color"
Now In aspx page i have one textbox and one asp.net button
Now In Master_Color Table has following Data
Black
Tan
Red
BlueNow User Enter in Textbox1 as any one of the below
Bllack
BBllack
Blackspace
spaceBlackWhatever he write we can find and show them alert because Black Already Exist
How to do using c# code behind on button click
Thanking You
Sunday, November 3, 2019 5:03 PM
All replies
-
User-1780421697 posted
static void Main(string[] args) { int f = 0; Console.WriteLine("enter the string"); string s = Console.ReadLine(); Console.WriteLine("enter the word to be searched"); string a = Console.ReadLine(); int l = s.Length; int c = a.Length; for (int i = 0; i < l; i++) { if (s[i] == a[0]) { for (int K = i + 1, j = 1; j < c; j++, K++) { if (s[K] == a[j]) { f++; } } } } if (f == c - 1) { Console.WriteLine("matching"); } else { Console.WriteLine("not found"); } Console.ReadLine(); }
Monday, November 4, 2019 2:11 AM -
User288213138 posted
Hi Gopi.MCA,
Now User Enter in Textbox1 as any one of the below
Bllack
BBllack
Blackspace
spaceBlackAccording to your description, i made demo for you.
I use a nested loop to compare 2 strings.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> protected void Button1_Click(object sender, EventArgs e) { string query = "SELECT color FROM Test62"; string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { int j = 0; int alert = 0; string search = TextBox1.Text; string data = sdr["color"].ToString(); for (int i = 0; i < data.Length; i++) { while (j < search.Length) { if (data[i] == search[j]) { j++; alert++; break; } else { j++; } } } if (alert == data.Length) { ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Black Already Exist');", true); } } } con.Close(); } }
The result:
Best regards,
sam
Monday, November 4, 2019 10:31 AM -
User-807418713 posted
Hello
Thanks
Some time user will type space then he is writing Black that time as well i want to show alert
Tuesday, November 5, 2019 5:02 AM -
User288213138 posted
Hi Gopi.MCA,
My code above can still meet your requirement.
The result:
Best regards,
Sam
Tuesday, November 5, 2019 8:49 AM -
User-807418713 posted
Hello
If I Type Red Then also its showing alert message
How to solve this
Thanking You
Wednesday, December 4, 2019 11:53 AM -
User288213138 posted
Hi Gopi.MCA,
Gopi.MCA
If I Type Red Then also its showing alert messageMy code above can still meet your requirement.
protected void Button1_Click(object sender, EventArgs e) { string query = "SELECT color FROM Test62"; string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { int j = 0; int alert = 0; string search = TextBox1.Text; string data = sdr["color"].ToString(); for (int i = 0; i < data.Length; i++) { while (j < search.Length) { if (data[i] == search[j]) { j++; alert++; break; } else { j++; } } } if (alert == data.Length) { string s = TextBox1.Text + " Already Exist"; ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + s + "');", true); } } } con.Close(); } } }
The result:
Best regards,
Sam
Thursday, December 5, 2019 1:55 AM -
User-807418713 posted
Hello
Thank You For Your New Code
1) If I user type Blackkk it has to show alerady exist coz black is in database
2) If user enter space then type Black have to show already exists alert.
3) If user put something Black,Tan then alert will not show
How to do so...
Thanking You
Thursday, December 5, 2019 6:20 AM -
User288213138 posted
Hi Gopi.MCA,
1) If I user type Blackkk it has to show alerady exist coz black is in database
2) If user enter space then type Black have to show already exists alert.
Do you want to have different alert based on different input values?
3) If user put something Black,Tan then alert will not showI don't understand what you mean, can you describe it in detail?
Best regards,
Sam
Friday, December 6, 2019 1:32 AM -
User-807418713 posted
Hello
Let's Assume This my sql query "Select ColorName from Colors"
Now It shows Result Set Like This
Black
Red
Now in aspx page I have One Textbox and one button
1) Now user enter Black in textbox and press button i want to show "Data Already Exist"
2) User Is typing spaceBlack then also I want to show "Data Already Exist"
3) User Is typing Blackspace then also I want to show "Data Already Exist"
3) User Is typing Bllack then also I want to show "Data Already Exist"
4) User Is typing Blacck then also I want to show "Data Already Exist"
) User Is typing Bl ack then also I want to show "Data Already Exist"
5) User Is typing Black,Red then I dont want to show alert because its a different color name
*Note : space is user sometimes pressing space button before typing that i want to check
Hope This is clear
Thanking You
Friday, December 6, 2019 3:30 AM -
User288213138 posted
Hi Gopi.MCA,
1) Now user enter Black in textbox and press button i want to show "Data Already Exist"
2) User Is typing spaceBlack then also I want to show "Data Already Exist"
3) User Is typing Blackspace then also I want to show "Data Already Exist"
3) User Is typing Bllack then also I want to show "Data Already Exist"
4) User Is typing Blacck then also I want to show "Data Already Exist"
) User Is typing Bl ack then also I want to show "Data Already Exist"
My code above meets these conditions.
5) User Is typing Black,Red then I dont want to show alert because its a different color nameIf you want to enter two colors and not alert, you can try the following code.
protected void Button1_Click(object sender, EventArgs e) { string query = "SELECT color FROM Test62"; string constr = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { int j = 0; int alert = 0; string search = TextBox1.Text; string data = sdr["color"].ToString(); for (int i = 0; i < data.Length; i++) { while (j < search.Length) { if (data[i] == search[j]) { j++; alert++; break; } else { j++; } } } if (alert == data.Length) { string s = TextBox1.Text + " Already Exist"; if (search.Contains(",")) { return; } else { ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + s + "');", true); } } } } con.Close(); } } }
Best regards,
sam
Friday, December 6, 2019 9:30 AM -
User753101303 posted
Hi,
If you need further help a higher level requirement could be usefull. For now it seems you want to prevent an entry which includes a word found in a list.
But what about colors such as "Blue", "PaleBlue", "LightBlue" etc... ?
It is also a bit harder to process mispelled values such as "Bllack" ie at which difference level do you consider this to be the same? At some point you have to trust users (a user could enter "abcd" as a color name if he wants).
Depending on what you are trying to I would enter all allowed color names in a table and would let the user to pick one. A trusted admin level user could enrich this table.
Edit: or this is homework ?
Friday, December 6, 2019 9:50 AM -
User-807418713 posted
Hello
Thanks for your reply
Simply task not allow user to enter the same color again if he enter anything space color name or color name spelling mistake
Then alert simple and that's it.
Thanking YouSunday, December 8, 2019 7:39 AM -
User475983607 posted
Gopi.MCA
Simply task not allow user to enter the same color again if he enter anything space color name or color name spelling mistake
Then alert simple and that's it.One very simple and common solution is displaying a list of colors and letting the user select a color from the list.
This is not a simple requirement if the user enters free formed text. If you think there is a an easy solution, then, IMHO, you have not put in enough effort to solve this requirement.
I see the solution as two steps. The first step is spell checking. Find a dictionary or a service that checks the user's spelling. Provide user feedback to correct any misspelled words. The next step is adding a unique constraint to the table column or writing a basic query to check if the color name exists.
Lastly, share your code. Most of your posts do not include source code. This infers you are not trying to learn and only want free code.
Sunday, December 8, 2019 2:09 PM -
User-807418713 posted
Hello
My Requirment Is Very Simple
database table has color Black
Sometimes user enter Bllack or Bl ack or spaceblack so its getting repeated again
for that i want to block user not to enter again same name
Thanking You
Thursday, December 12, 2019 6:18 PM -
User475983607 posted
My Requirment Is Very Simple
database table has color Black
Sometimes user enter Bllack or Bl ack or spaceblack so its getting repeated again
for that i want to block user not to enter again same name
The response indicates you put no effort into designing a solution otherwise you would understand the complexity.
Thursday, December 12, 2019 6:43 PM -
User409696431 posted
The requirement to avoid spaces before and/or after the user entry is very simple. Don't use TextBox1.Text, use TextBox1.Text.Trim(). That will cause " Black", " Black ", "Black ", and so on to become "Black".
You also have to consider that "Black" doesn't equal "black", which a user could enter. You could make all the colors always be lowercase by using TextBox1.Trim().ToLower(), or always be uppercase by using TextBox1.Text.Trim().ToUpper() .
The requirement to correct spelling mistakes is NOT simple. You'd have to know if words were incorrectly spelled - and that wouldn't prevent a user from entering a correctly spelled word that was not a color ("back" instead of "black" for example). If you want to constrain inputs to a list of properly spelled colors, you should generate a list of allowed colors and then it would be easy to compare the inputs to that list and reject any inputs that don't match an allowed color. Of course, in that case, why not just use a dropdownlist of the allowed color choices and make it simple for the user to enter an acceptable entry?
Friday, December 13, 2019 4:41 AM