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:
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:
ALTER PROCEDURE [DBO].P_FN_PR_ValidRecipe
AS
select DISTINCT port_recipe_num
from FN_Portions
RETURN
ASPX Code:
<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>