2011-09-21 67 views
0

我想用最好的例子之一,從下面的網站檢查用戶名是否可以 http://www.highoncoding.com/Articles/439_Performing_Instant_UserName_Availability_Check_Using_JQuery_Ajax_API.aspx即時用戶名檢查可用性使用數據庫asp.net

,它只是用一些預先啓動的名字,但我驗證不想嘗試使用我的數據庫,任何人都可以告訴我如何進一步處理。

下面是代碼:

<script language="javascript" type="text/javascript"> 

var userName = ''; 

$(document).ready(function() 
{ 
    $("#txtUserName").blur(function() 
    { 
     userName = $(this).val(); 

     if(userName.length <= 6) 
     { 
      $("#display").text("username must be atleast 7 characters"); 
      $("#display").css("background-color","red"); 
     } 

     else 
     { 
      $.ajax(
      { 
       type:"POST", 
       url:"AjaxService.asmx/CheckUserNameAvailability", 
       data:"{\"userName\":\"" + userName + "\"}", 
       dataType:"json", 
       contentType:"application/json", 
       success: function(response) 
       { 
        if(response.d == true) 
        { 
         $("#display").text("username is available"); 
         $("#display").css("background-color","lightgreen"); 
        } 
        else 
        { 
         $("#display").text("username is already taken"); 
         $("#display").css("background-color","red"); 
        } 
       } 
      }); 
     } 


    }); 
}); 


</script> 

這AjaxService.asmx:

Imports System.Collections 
Imports System.ComponentModel 
Imports System.Data 
Imports System.Linq 
Imports System.Web 
Imports System.Web.Services 
Imports System.Web.Services.Protocols 
Imports System.Xml.Linq 
Imports System.Collections.Generic 

Namespace JQueryUserNameAvailability 


    <WebService([Namespace] := "http://tempuri.org/")> _ 
    <WebServiceBinding(ConformsTo := WsiProfiles.BasicProfile1_1)> _ 
    <ToolboxItem(False)> _ 
    <System.Web.Script.Services.ScriptService> _ 
    Public Class AjaxService 
     Inherits System.Web.Services.WebService 
     <WebMethod> _ 
     Public Function CheckUserNameAvailability(userName As String) As Boolean 
      Dim userNames As List(Of [String]) = New List(Of String)() From { _ 
       "azamsharp", _ 
       "johndoe", _ 
       "marykate", _ 
       "alexlowe", _ 
       "scottgu" _ 
      } 

      Dim user = (From u In userNames Where u.ToLower().Equals(userName.ToLower())u).SingleOrDefault(Of [String])() 

      Return If([String].IsNullOrEmpty(user), True, False) 
     End Function 

    End Class 
End Namespace 

修改後的代碼:

Public Function CheckUserNameAvailability(ByVal userName As String) As Boolean 
    Dim strSql As String = String.Format("SELECT COUNT(UserNameCol) FROM Registration WHERE UserNameCol = '{0}'", userName) 
    Dim strConnection As String = "Data Source=.\sqlexpress;Initial Catalog=Users;Integrated Security=True" 
    Dim sqlConn As New SqlConnection(strConnection) 
    Dim sqlDataAdap As New SqlDataAdapter(strSql, sqlConn) 
    Dim dt As New DataTable() 
    sqlDataAdap.Fill(dt) 

    If Convert.ToInt32(dt.Rows(0)(0)) > 0 Then 
     Return False 
    End If 

    Return True 
End Function 

回答

1

修改您CheckUserNameAvailability函數從表中獲取數據,你已保存所有用戶名。一個例子:

Public Function CheckUserNameAvailability(userName As String) As Boolean 
      Dim strSql as String = String.Format("SELECT COUNT(userNameCol) FROM users WHERE userNameCol = '{0}'", userName) 
      Dim sqlConn as new SqlConnection(SQL Connection String) 
      Dim sqlDataAdap as new SqlDataAdapter(strSql, sqlConn) 
      Dim dt as new DataTable() 
      sqlDataAdap.Fill(dt) 

      If Convert.ToInt32(dt.Rows(0)(0)) > 0 Then 
       Return false 
      End If 

      Return true 
     End Function 
+0

感謝您的回覆我已修改如上所示,但它不工作。 – coder

+0

你收到的錯誤是什麼?你是否正確設置了連接字符串? – Waqas

+0

我沒有收到任何錯誤,這是我的conn字符串 coder