how to check the username is already exists in table...!

  • 11 years ago

     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.

     
    my code is:

    <script language="VB" runat="server">
       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(Sender As Object, E As EventArgs)
         If Page.IsValid 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()
         End If
       End Sub
       </script>
     i will look forward for your positive response.

     

    Regards,

    Asad_Black 

  • 11 years ago

    Before Insert, Create an Query like this;

         Select count(*) From login where UserName like 'Parameter' [ ur parameter]...

    if the query returns more than 0 , then the user is avail else not...

     

    In case of Sql Server, Use if Exists( Select UserName from Login where UserName like '@Username')

    i.e in both cases before insert , pass the username parameter to ur query and check the count is having 0 or not....

    if 0 - new insert... else already exists

  • 11 years ago

     this is my code and it does not work.

    i dont know whatz de problem in this code.
     

     

     

    <script language="VB" runat="server">
       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
     
       </script>

    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

  • 10 years ago

    Try openning the connection before assigning it to command object.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Memory is like an orgasm. It's a lot better if you don't have to fake it.” - Seymour Cray