注册 登录
编程论坛 ASP.NET技术论坛

帮助检查注册页面中验证用户名是否为重的.net vb版的代码

cannie2008 发布于 2008-07-14 09:21, 810 次点击
新手刚学编程,做注册页面  其中用户名需要验证是否已被用,我使用的customvalidation控件,出现的问题是无论是输入什么值都是反应errormessage的值 代码如下:
控件的属性如下:
clientValidationFunction=CustomValidator1_ServerValidate;
controltovalidate=username;//usernanme 为textbox的id
enableclientscipt=true
enabled=true;
enableviewstate=true
visible=true  
errormessage=已被注册   
 不知是不是控件属性没有设好

函数代码:
Private Sub CustomValidator1_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim connstring As String = "data source=JZHOST2\SQL1;initial catalog=piman; user id=sa;pwd=cannie"
        Dim myconn As New SqlConnection(connstring)
        myconn.Open()
           
        Dim s As String
        s = "select username from huiyuan"  huiyuan为数据表,验证username 是否被用
        Dim mycommand As New SqlDataAdapter(s, myconn)

        Dim ds As DataSet
        ds = New DataSet
        mycommand.Fill(ds, "huiyuan")

        Dim dv As DataView
        dv = ds.Tables(0).DefaultView
        Dim datarow As DataRowView

        Dim txt As String
        args.IsValid = True
        For Each datarow In dv
            txt = datarow.Item("username").ToString()
            If txt = args.Value Then
                args.IsValid = False
                Exit For
            End If
        Next
    End Sub


大家帮忙看看 多谢  多谢

[[it] 本帖最后由 cannie2008 于 2008-7-14 09:29 编辑 [/it]]
0 回复
1