Question:
VB NET when user enters information into text box - stop duplication?
2012-04-20 04:11:33 UTC
Hi, Is there a code that when a user enters information into the text box that is already stored in a database a message appears to ask the user to change the number entered.

For instance a record stored in database 1
stop user typing in 1 and replacing the textbox 1 in the database when update clicked
Three answers:
2012-04-20 06:23:06 UTC
Let the database do the work for you. Set a unique key on the field and catch the error from the database when you violate the constraint and pop up an error warning to the user.



You can also enforce constraints on a datatable within your applications if you're pulling data in a dataset.



Alternately you could query the database for a count on that value in that field before trying to write to the database, if count is greater than 0 don't write to the DB and notify the user.



What you do NOT want to do is loop through a database one record at a time ( as in the example above ). That may not cause a problem with a very small database, but you are going to see a pretty serious performance hit on any database with a bit of size.
Blue (Abhi)
2012-04-20 05:51:51 UTC
Its a easy task..



use a text change event for that textbox...



when a text change event occurs,store that in integer variable,then move the recordset to first and use a while loop to compare with each row... if it find then say a message that it already exists and clear the textbox using Tex1.text=""



assume there is a roll number field in a database called "rno"...and i have use the adodc control to connect the database



private sub text1_change()

dim n as integer

n=val(text1.text) //n contains number value from text box

adodc1.recordset.movefirst //move the control to first record

while(adodc1.recordset != EOF) //while loop to see if it is End of file

if (adodc1.recordset.fields("rno")= n) then //check if n value matches with the rno field value

msgbox"Already exists" //if exists msgbox pops

text1.text="" //makes the text box null

else

adodc1.recordset.movenext //if it not equal,then move to next record

endif

wend

end sub



the while loop runs until the eof file,if number doesnt matches with the rno field,no thing happens,if it matches it clears the text box and gives a message that it already exists..



hope it helps
?
2016-10-22 07:50:40 UTC
This code is the position you want to make your transformations. If IsValidInput(txtWidth.textual content) Then width = txtWidth.textual content end If If IsValidInput(txtLength.textual content) Then length = txtLength.textual content end If CalculateAndDisplay(width, length) i'm assuming that IsValidInput() is a function that checks for characters. This function also needs to placed the textual content contained in the label field. yet then you definately damage issues by ability of always calling the CalculateAndDisplay subroutine. attempt this. If IsValidInput(txtWidth.textual content) Then width = txtWidth.textual content If IsValidInput(txtLength.textual content) Then length = txtLength.textual content CalculateAndDisplay(width, length) end If end If Now both fields might want to be valid before the calculate sub is called. yet as IsValidInput is a VB function why no longer write your own version If IsValidWidth(txtWidth.textual content) Then width = txtWidth.textual content If IsValidLength(txtLength.textual content) Then length = txtLength.textual content CalculateAndDisplay(width, length) end If end If have relaxing.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...