根据数据库验证文本框值 [英] validate textbox value against a database

查看:62
本文介绍了根据数据库验证文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我想针对数据库验证我的文本框值.
当用户在texbox中输入值时,我想对照我的数据库值对其进行检查.
如果数据库中存在值,则允许用户输入该值,否则显示错误,表明该值不存在.
任何人都可以有任何示例代码.如何进行这种验证.
任何帮助表示赞赏.

作者更新:

我正在使用下面的函数来验证文本框,但这不是在验证文本框值.
感谢您在代码中找不到我所需要的任何帮助.

VB代码:

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代码:



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>

推荐答案

首先,您需要向我们展示您的尝试,以便我们可以帮助您将流程分解为适当的步骤.
如果您付出一些努力,您会惊讶于您的无所不能.当您遇到困难时,我们会在这里鼓励和指导您,但只为您提供剪切和粘贴的答案不会对我们或您有任何帮助.
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.


您的这个问题太笼统了.您应该首先研究如何从所使用的特定数据库访问数据.您可以找到许多代码项目文章 [ google. [
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.


Re:如果数据库中存在值,则允许用户输入该值

为什么不只是使用一个填充有数据库可接受输入值的下拉列表,那么您将知道该值存在于数据库中,并且用户将无法输入不可接受的值.
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.


这篇关于根据数据库验证文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆