Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to validate my textbox value against a database.
when user input value in texbox , i want to check it against my database value.
if value exists in database, user is allowed to input that value , else shows an error, that value doesn't exists.
Can anyone have any sample code. how to do this kind of validation.
Any help is appreciated.

Author's Update:

I am validating my textbox using below function, but it wasn't validating textbox value.
Appreciate for any help to find what i am missing in my code.

VB Code:

VB
Protected Sub ValidateRecipe(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
       Dim con11 As New SqlConnection(ConfigurationManager.ConnectionStrings("FConnectionString").ConnectionString)
       con11.Open()
       Dim ds11 As New DataSet()
       Dim ada11 As New SqlDataAdapter("P_FN_PR_ValidRecipe", con11)
       ada11.SelectCommand.CommandType = CommandType.StoredProcedure
       ada11.Fill(ds11, "P_FN_PR_ValidRecipe")
       Dim dv As DataView
       dv = ds11.Tables(0).DefaultView
       Dim datarow As DataRowView
       Dim txtRecipe As String
       args.IsValid = False
       For Each datarow In dv
           txtRecipe = datarow.Item("port_recipe_num").ToString()
           If txtRecipe = args.Value Then
               args.IsValid = True
               Exit For
           End If
       Next
   End Sub



Stored Procedure:

SQL
ALTER PROCEDURE [DBO].P_FN_PR_ValidRecipe
AS
select DISTINCT port_recipe_num
from FN_Portions
RETURN


ASPX Code:
XML
<asp:TextBox ID="TextBox1" runat="server" MaxLength="4" Width="150px"
                        Height="16px" AutoPostBack="True" CausesValidation="True"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"
                        ErrorMessage="* required" ForeColor="#CC0000" Display="Dynamic"></asp:RequiredFieldValidator>
                    <br />
                    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1"
                        OnServerValidate="ValidateRecipe" Display="Dynamic" ForeColor="#CC0000"
                        ErrorMessage="No Recipe # Exist !"></asp:CustomValidator>
Posted
Updated 24-Jan-11 15:41pm
v2
Comments
Venkatesh Mookkan 24-Jan-11 21:42pm    
Updated the question using the answer provided by the Author.

@Rohit, you are supposed to Improve question unless you have answer. Please do not add answer.

First off you need to show us what you have tried so that we can help you with how to break down the process into appropriate steps.
You'll be amazed at what you can do if you put in some effort. We're here to encourage and instruct you when you run into difficulties, but it won't help us or you at all to just give you a cut and paste answer.
 
Share this answer
 
Your question is too general. You should start by researching how to access data from the specific database you are using. You can find many code project articles[^] on the subject. Another good way to start is to use google.[^]

Once you've gotten started, if you have a more specific question come on back and post it with the relevant code you're working with and you will get a lot more help.
 
Share this answer
 
Re: if value exists in database, user is allowed to input that value

why not just use a dropdownlist populated with acceptable input values from the database, then you will know the value exists in the database and the user won't be able input unacceptable values.
 
Share this answer
 
Comments
Rohit.Net100 24-Jan-11 18:05pm    
records in database field is more than 100+

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