I am having a problem that dtAsia.rows.count>0 doesn't count at all. All I want to do is tell the user that their username already exists in the game but I'm having a bad time now because for example: I entered a valid username and its success... I entered an invalid username and got the CATCH part of this code.. So if i entered a valid username again. I still got the CATCH part of the code until the end even if I enter valid or invalid username still got the CATCH part. What did I do wrong here? I will really appreciate answers. Thank you

If (dtAsia.Rows.Count > 0) Then ' wont work
            MsgBox("Username: " & Trim(txtUsername.Text) & " is already exist.", MsgBoxStyle.OkOnly, "Message :")
        Else
            Try
                Call open_conn()
                SqlComm.Connection = conn
                SqlComm.CommandText = "Select * from Personal_Information"
                SqlComm.CommandText = "INSERT INTO Personal_Information(Username) values('" & Trim(txtUsername.Text) & "')
                SqlComm.ExecuteNonQuery()
                Call close_conn()
                MessageBox.Show("Account registered successfully!", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Call Updater()
            Catch ex As Exception 'Here is the CATCH PART
                MsgBox("error " & Trim(txtUsername.Text) & " is already exist.", MsgBoxStyle.OkOnly, "Message :")
            End Try
        End If

Dani AI

Generated

Common causes for dtAsia.Rows.Count not behaving as expected are that the DataTable/DataSet was never filled, the wrong object was checked (DataSet vs DataTable), or the check happens before the query runs. 's suggestion to step through with the debugger and 's note about removing the TRY to see the real error are exactly the right troubleshooting moves. Confirm the object is initialized (if it is a DataSet check Tables.Count > 0 first), inspect the SQL used to populate it, and verify the username value is trimmed/normalized before the lookup.

A more robust pattern is to check existence with a parameterized scalar query, then run an insert only when the scalar returns zero. Parameterized commands prevent SQL injection and Using blocks ensure proper disposal of connections/commands:

Using conn As New SqlClient.SqlConnection(connectionString)
  Using cmd As New SqlClient.SqlCommand("SELECT COUNT(1) FROM Personal_Information WHERE Username = @u", conn)
    cmd.Parameters.Add("@u", SqlDbType.NVarChar, 50).Value = txtUsername.Text.Trim()
    conn.Open()
    Dim exists As Boolean = Convert.ToInt32(cmd.ExecuteScalar()) > 0
    If exists Then
      MessageBox.Show("Username already exists.")
    Else
      Using ins As New SqlClient.SqlCommand("INSERT INTO Personal_Information (Username) VALUES (@u)", conn)
        ins.Parameters.Add("@u", SqlDbType.NVarChar, 50).Value = txtUsername.Text.Trim()
        ins.ExecuteNonQuery()
      End Using
    End If
  End Using
End Using

Enforce uniqueness at the database level to avoid race conditions (example SQL):

ALTER TABLE Personal_Information
ADD CONSTRAINT UQ_Personal_Information_Username UNIQUE (Username);

When a unique constraint is present, catch SqlException and check error numbers (2627/2601) to present a friendly duplicate-message rather than relying on a generic catch. Also prefer explicit parameter types over AddWithValue, normalize case if the app requires case-insensitive matches, and use breakpoints to inspect the DataTable/DataSet contents as and suggested if problems continue.

Recommended Answers

All 6 Replies

if possible can you please attach the project here.....

I guess that dtAsia is a dataset you've filled using the username as a criteria, so 0 rows means you've got to insert the user in personal_information.
unfortunately you are not providing what goes on when filling the dtAsia, so my only suggestion would be to step through the code at debug and see where the jump to catch happens and then msgbox yourself the exception to see what goes wrong.

PS: I don't understand why you are first setting the command to a select and then changing it to an insert, but this couldn't be the problem.

oh... I already this figured out.. I removed the TRY statement to see the exact error.. and i got it.. thanks anyways

commented: thats the learning process +5

If your problem is solved make the thread as solved....

Dim sqlcon As New SqlClient.SqlConnection(My.Settings.CLSHWSETAConnectionString.ToString)
Dim objCom As New SqlClient.SqlCommand(strquery, sqlcon)
sqlcon.Open()

Now you know where the error is, try solving it and if you still encounter further problems, you can surely get your answer here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.