Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SqlException try/catch in Dreamweaver

Status
Not open for further replies.

marthirial

IS-IT--Management
Jan 29, 2005
20
0
0
US
After deciding to use the SQLException for checkign duplicated usernames, my application would do something like:

- new member enters username
- submits form
- SQL receives data and creates a SQLException if username is already existing.
- Page redirects, according to the kind of SQLExpection to the same form and shows the error by using an asp.label

I know that I have to use a Try/Catch error handling, but I don't have the knowledge of how to write the statement or where to put this code since I am inserting the record using the "Insert Record" Server behavior in dreamweaver.

My server model is ASP.NET/VB.NET using SQL server

Thanks a lot for the input.
 
A SQLException will only be thrown when the database reports an error (e.g. an invalid SQL statement, a timeout, violating a primary key etc). If you are just checking for an existing username you should never receive a SQLException unless something like the above happens.

Try/Catch blocks are there to catch any unexpected errors so you can use them to do this but again not to see if you username already exist.

What I would suggest is to create a DataReader that you can read to see if a username exists. e.g
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim MyDataReader As System.Data.SQLClient.SqlDataReader
        Dim MyCommand As System.Data.SQLClient.SqlCommand
        Dim MyConnection As System.Data.SQLClient.SqlConnection
        Dim bolUserExists As Boolean = False
        Try
            ' Open a connection to the database
            MyConnection = New System.Data.SQLClient.SqlConnection("server=127.0.0.1; initial catalog=MyDatabase;uid=USERNAME;pwd=PASSWORD")
            MyConnection.Open()
            MyCommand = New System.Data.SQLClient.SqlCommand
            MyCommand.Connection = MyConnection
            ' Create a SQL Statement for the command object
            ' that will look for a username based on what the
            ' user types in Textbox1
            MyCommand.CommandText = "SELECT USERNAME FROM MYUSERTABLE WHERE USERNAME = '" & TextBox1.Text & "'"
            MyDataReader = MyCommand.ExecuteReader()
            ' Now read through the returned results
            While MyDataReader.Read
                ' If any results are returned we set a flag to True
                ' to say that the user already exists
                bolUserExists = True
            End While
            MyDataReader = Nothing
            MyCommand = Nothing
            MyConnection.Close()
            MyConnection = Nothing

            If bolUserExists = False Then
                Response.Write("Username not in use")
            Else
                Response.Write("Username already exists")
            End If

        Catch ex As Exception
            Response.Write("Error:" & ex.Message)
        End Try

    End Sub

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
THanks a lot, I will implement this routine and see how it works for my needs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top