Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. how to check the username is already exist in a table...?

how to check the username is already exist in a table...?

Scheduled Pinned Locked Moved ASP.NET
databasehelpsysadmintutorialquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    asad_black
    wrote on last edited by
    #1

    hi every one...! em creating a create user account page. i want to check the username is already exists in my database table or not? if exists then give me a error message that username is already exists if not then account will be successfully create. em using this code but its not work correctly.. i dont know whatz de problem. and please remember em using Access database. please point out my mistakes. Dim objConnection As OleDbConnection Sub Page_Load(Source As Object, E As EventArgs) objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " _ & "DATA SOURCE=" _ & Server.MapPath("nwind.mdb;")) End Sub Sub btnInsert_Click(ByVal Sender As Object, ByVal E As EventArgs) Dim query As String query = "Select Count(*) From login Where username = ? " Dim result As Integer = 0 If result > 0 Then Dim strSQL As String = "INSERT INTO login (username, [password]) " & _ "VALUES (?, ?) " Dim dbComm As New OleDbCommand(strSQL, objConnection) dbComm.Parameters.Add("UserName", OleDbType.VarChar, 32, "UserName") dbComm.Parameters.Add("Password", OleDbType.VarChar, 128, "Password") dbComm.Parameters("UserName").Value = txtUserName.Text dbComm.Parameters("Password").Value = txtPassword.Text Try objConnection.Open() dbComm.ExecuteNonQuery() Catch ex As Exception Response.Write(ex.Message) Response.End() Finally If objConnection.State = ConnectionState.Open Then objConnection.Close() End If End Try Response.Write("A new record has been added") Response.End() Else Response.Write("your username is already exist") End If End Sub

    C P N 3 Replies Last reply
    0
    • A asad_black

      hi every one...! em creating a create user account page. i want to check the username is already exists in my database table or not? if exists then give me a error message that username is already exists if not then account will be successfully create. em using this code but its not work correctly.. i dont know whatz de problem. and please remember em using Access database. please point out my mistakes. Dim objConnection As OleDbConnection Sub Page_Load(Source As Object, E As EventArgs) objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " _ & "DATA SOURCE=" _ & Server.MapPath("nwind.mdb;")) End Sub Sub btnInsert_Click(ByVal Sender As Object, ByVal E As EventArgs) Dim query As String query = "Select Count(*) From login Where username = ? " Dim result As Integer = 0 If result > 0 Then Dim strSQL As String = "INSERT INTO login (username, [password]) " & _ "VALUES (?, ?) " Dim dbComm As New OleDbCommand(strSQL, objConnection) dbComm.Parameters.Add("UserName", OleDbType.VarChar, 32, "UserName") dbComm.Parameters.Add("Password", OleDbType.VarChar, 128, "Password") dbComm.Parameters("UserName").Value = txtUserName.Text dbComm.Parameters("Password").Value = txtPassword.Text Try objConnection.Open() dbComm.ExecuteNonQuery() Catch ex As Exception Response.Write(ex.Message) Response.End() Finally If objConnection.State = ConnectionState.Open Then objConnection.Close() End If End Try Response.Write("A new record has been added") Response.End() Else Response.Write("your username is already exist") End If End Sub

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      asad_black wrote:

      Dim query As String query = "Select Count(*) From login Where username = ? " Dim result As Integer = 0 If result > 0 Then

      Perhaps running the query against your DB is a good move ?

      Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.

      1 Reply Last reply
      0
      • A asad_black

        hi every one...! em creating a create user account page. i want to check the username is already exists in my database table or not? if exists then give me a error message that username is already exists if not then account will be successfully create. em using this code but its not work correctly.. i dont know whatz de problem. and please remember em using Access database. please point out my mistakes. Dim objConnection As OleDbConnection Sub Page_Load(Source As Object, E As EventArgs) objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " _ & "DATA SOURCE=" _ & Server.MapPath("nwind.mdb;")) End Sub Sub btnInsert_Click(ByVal Sender As Object, ByVal E As EventArgs) Dim query As String query = "Select Count(*) From login Where username = ? " Dim result As Integer = 0 If result > 0 Then Dim strSQL As String = "INSERT INTO login (username, [password]) " & _ "VALUES (?, ?) " Dim dbComm As New OleDbCommand(strSQL, objConnection) dbComm.Parameters.Add("UserName", OleDbType.VarChar, 32, "UserName") dbComm.Parameters.Add("Password", OleDbType.VarChar, 128, "Password") dbComm.Parameters("UserName").Value = txtUserName.Text dbComm.Parameters("Password").Value = txtPassword.Text Try objConnection.Open() dbComm.ExecuteNonQuery() Catch ex As Exception Response.Write(ex.Message) Response.End() Finally If objConnection.State = ConnectionState.Open Then objConnection.Close() End If End Try Response.Write("A new record has been added") Response.End() Else Response.Write("your username is already exist") End If End Sub

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        asad_black wrote:

        Dim query As String query = "Select Count(*) From login Where username = ? " Dim result As Integer = 0 If result > 0 Then

        Well, there's a pretty good start. You've defined a query and then you don't use it anywhere. You initialise result to 0, and then test to see if result is greater than zero. Are you seeing the problems with that?

        asad_black wrote:

        Dim strSQL As String = "INSERT INTO login (username, [password]) " & _ "VALUES (?, ?) " Dim dbComm As New OleDbCommand(strSQL, objConnection) dbComm.Parameters.Add("UserName", OleDbType.VarChar, 32, "UserName") dbComm.Parameters.Add("Password", OleDbType.VarChar, 128, "Password")

        Here you define your query with ? placeholders and then don't use them to when assigning your parameters. There are lots of things wrong here, and you should take the time to look into it.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        1 Reply Last reply
        0
        • A asad_black

          hi every one...! em creating a create user account page. i want to check the username is already exists in my database table or not? if exists then give me a error message that username is already exists if not then account will be successfully create. em using this code but its not work correctly.. i dont know whatz de problem. and please remember em using Access database. please point out my mistakes. Dim objConnection As OleDbConnection Sub Page_Load(Source As Object, E As EventArgs) objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " _ & "DATA SOURCE=" _ & Server.MapPath("nwind.mdb;")) End Sub Sub btnInsert_Click(ByVal Sender As Object, ByVal E As EventArgs) Dim query As String query = "Select Count(*) From login Where username = ? " Dim result As Integer = 0 If result > 0 Then Dim strSQL As String = "INSERT INTO login (username, [password]) " & _ "VALUES (?, ?) " Dim dbComm As New OleDbCommand(strSQL, objConnection) dbComm.Parameters.Add("UserName", OleDbType.VarChar, 32, "UserName") dbComm.Parameters.Add("Password", OleDbType.VarChar, 128, "Password") dbComm.Parameters("UserName").Value = txtUserName.Text dbComm.Parameters("Password").Value = txtPassword.Text Try objConnection.Open() dbComm.ExecuteNonQuery() Catch ex As Exception Response.Write(ex.Message) Response.End() Finally If objConnection.State = ConnectionState.Open Then objConnection.Close() End If End Try Response.Write("A new record has been added") Response.End() Else Response.Write("your username is already exist") End If End Sub

          N Offline
          N Offline
          naveedmazhar
          wrote on last edited by
          #4

          asad_black wrote:

          If result > 0 Then

          I think if you will modify your condition mentioned above to: If result = 0 Then then it will work.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups