2012-12-20 3 views
1

사용자 이름이 이미 존재하는지 확인하고 싶습니다. 이것은 내가 도달했지만 작동하지 않는 것입니다.signUp시 존재하는 사용자 이름을 확인하는 방법

Dim cmdstr As String = "Select count(*) from Registration where username = '" & txtName.Text & "'" 
Dim userExist As SqlCommand = New SqlCommand(cmdstr, con) 
Dim temp As Integer = Convert.ToInt32(userExist.ExecuteScalar().ToString()) 
    If (temp = 1) Then 
     Response.Write("user name is already Exist!!") 
    End If 
+1

** SQL INJECTION **, look it up. 오류 란 무엇이며 왜 "작동하지 않습니까?" –

+0

메시지를주지 않습니다. – Hassan

답변

1
  1. SQL-Injection에 대한 귀하의 개방. SQL과 쿼리 문자열을 연결하지만 SqlParameters
  2. 을 사용하여 당신은 연결 (난 가정)

여기에 전체 샘플하였습니다하지 마십시오 :

Public Shared Function GetUserCount(userName As String) As Int32 
    Const sql = "SELECT COUNT(*) FROM Registration where username = @UserName" 
    Using con As New SqlConnection(connectionString) 
     Using cmd = New SqlCommand(sql, con) 
      cmd.Parameters.AddWithValue("@UserName", userName) 
      con.Open() 
      Using reader = cmd.ExecuteReader() 
       If reader.HasRows 
        reader.Read() 
        Dim count As Int32 = reader.GetInt32(0) 
        Return count 
       End If 
      End Using 
     End Using 
    End Using 
End Function 

을하고있는 방법을 사용 이 방법은 :

Dim userCount As Int32 = GetUserCount(txtName.Text.Trim()) 
If userCount > 0 
    LblWarning.Text = "User-name already exists!" 
End If 
+0

고마워요. 나는 그것을 시도하지만 나 에게이 오류를 제공합니다 " 데이터가 존재하지 않을 때 잘못된 시도가 읽기." 여기를 참조하십시오. "Count As Int32 = reader.GetInt32 (0)" – Hassan

+0

@Hassan : 내 대답을 편집하면 먼저 'reader.HasRows' 속성을 사용해야합니다. –

+0

매력, 감사합니다 팀 억 시간 : 당신이 나를 구원^_ ^ – Hassan

관련 문제