Loading

how to use the primary key exeception in exceptional handling?

2 posts / 0 new
Last post
Offline
Joined: 05/23/2010
how to use the primary key exeception in exceptional handling?

i have one registration form which consists of username, password, confirmpassword and Register button.
When i will be typing the username, it should show whether the username is available or not. and i have changed the username column as primary key in sql database.

and also coding for primary key exception using exceptional handling.

so i need the coding for that requirement...

and also how to clear the text from the form...
thanks in advance..
please help me soon

please send the code to my email id: surendar_julakanti@yahoo.co.in

admin's picture
Offline
Joined: 11/14/2008
re: primary key exeception in exceptional handling

Use the following function to determine if the record exist on your table.

  1. Public Function CountRows(ByVal sSQL As String)
  2. Dim CN As SqlConnection
  3. CN= New SqlConnection
  4.  
  5. With CN
  6. If .State = ConnectionState.Open Then .Close()
  7.  
  8. .ConnectionString = cnString
  9. .Open()
  10. End With
  11.  
  12. Dim cmdCount As SqlCommand = New SqlCommand(sSQL, CN)
  13.  
  14. CountRows = cmdCount.ExecuteScalar()
  15. End Function

Call it using this code:

  1. Dim sSQL As String = "SELECT Count(*) FROM Users WHERE Username = '" & txtUsername.text & "'"
  2.  
  3. Dim RecordCount As Integer = CountRows(sSQL)
  4.  
  5. If RecordCount = 0 Then
  6. MsgBox("Username already exists.", vbInformation)
  7.  
  8. Exit Sub
  9. End If

Pages

Add new comment

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.