locked
How to add data from textbox to SQL Server Database using VB.net ASP.net RRS feed

  • Question

  • User-1146998100 posted

    Good day,

    I am creating a website using VB.net ASP.net and I want to check if the ID of the client is exsit or not. If it exsits a message appears in a label on the website and say "ID exsit" otherwise it adds the information to the users table.

    I can add the new data to the database but i need to check whether the ID exsits or not before excuting the insert code.

    Best Regards

    Wednesday, January 30, 2013 3:30 AM

Answers

  • User42903263 posted

    You just need to query the database, perhaps like this:

    Friend Function IdExists(id As String) As Boolean
        Dim exists As Boolean = False
        Dim conn As New SqlConnection("yourConnectionString")
        Dim sql = "SELECT COUNT(*) FROM [yourTableName] WHERE [Id]=@Id;"
        Dim sqlCmd As New SqlCommand(sql, conn)
        sqlCmd.Parameters.AddWithValue("@Id", id)
    
        Try
            conn.Open()
            exists = (CInt(sqlCmd.ExecuteScalar()) <> 0)
        Catch ex As Exception
            ' something went wrong '
            Throw ex
        Finally
            conn.Close()
        End Try
    
        Return exists
    
    End Function
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2013 3:24 PM

All replies

  • User1221318268 posted

    in the insert stored procedure just check for ID it is exist or not in the tables..

    if not exists then insert other wise display message like ID existed

     

    regards,

    thotkaura

    Wednesday, January 30, 2013 4:36 AM
  • User-1146998100 posted

    Thank you for your reply; but i need to do this using the vb.net in the webform not on the database.

    Wednesday, January 30, 2013 5:58 AM
  • User42903263 posted

    You just need to query the database, perhaps like this:

    Friend Function IdExists(id As String) As Boolean
        Dim exists As Boolean = False
        Dim conn As New SqlConnection("yourConnectionString")
        Dim sql = "SELECT COUNT(*) FROM [yourTableName] WHERE [Id]=@Id;"
        Dim sqlCmd As New SqlCommand(sql, conn)
        sqlCmd.Parameters.AddWithValue("@Id", id)
    
        Try
            conn.Open()
            exists = (CInt(sqlCmd.ExecuteScalar()) <> 0)
        Catch ex As Exception
            ' something went wrong '
            Throw ex
        Finally
            conn.Close()
        End Try
    
        Return exists
    
    End Function
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 30, 2013 3:24 PM
  • User-1718098015 posted

    Hi,

    I think you are trying to say that you dont want to make trip to database to check if ID exists or not, and decide from code itself. Unfortunately you cant do that. One way is as suggested by Andrew Morton in above post, other way is to populate all the IDs in some list at page load (it will be good only if you are calling database at pageload for some purpose) and check id entered in textbox using code itself.

    Thanks

    Nirav......HTH

    Thursday, January 31, 2013 6:09 AM
  • User-1146998100 posted

    Thank you everyone who replied to my post.

    Thank you Andrew for your help.

    I tried your code and made some changes to go with my code and it worked.

     

    Thursday, January 31, 2013 7:45 AM