问题描述
嗨我想针对数据库验证我的文本框值.
当用户在texbox中输入值时,我想对照我的数据库值对其进行检查.
如果数据库中存在值,则允许用户输入该值,否则显示错误,表明该值不存在.
任何人都可以有任何示例代码.如何进行这种验证.
任何帮助表示赞赏.
作者更新:
我正在使用下面的函数来验证文本框,但这不是在验证文本框值.
感谢您在代码中找不到我所需要的任何帮助.
VB代码:
HiI 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代码:
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>
推荐答案
这篇关于根据数据库验证文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!