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.
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 ahmedsaid50 on Jan 31, 2013 11:45 AM
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.
ahmedsaid50
Member
1 Points
3 Posts
How to add data from textbox to SQL Server Database using VB.net ASP.net
Jan 30, 2013 07:30 AM|LINK
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
ashkc
Participant
960 Points
200 Posts
Re: How to add data from textbox to SQL Server Database using VB.net ASP.net
Jan 30, 2013 08:36 AM|LINK
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
ahmedsaid50
Member
1 Points
3 Posts
Re: How to add data from textbox to SQL Server Database using VB.net ASP.net
Jan 30, 2013 09:58 AM|LINK
Thank you for your reply; but i need to do this using the vb.net in the webform not on the database.
Andrew Morto...
Member
360 Points
60 Posts
Re: How to add data from textbox to SQL Server Database using VB.net ASP.net
Jan 30, 2013 07:24 PM|LINK
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 FunctionNiravVyas
Participant
912 Points
217 Posts
Re: How to add data from textbox to SQL Server Database using VB.net ASP.net
Jan 31, 2013 10:09 AM|LINK
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
Thanks
Nirav
ahmedsaid50
Member
1 Points
3 Posts
Re: How to add data from textbox to SQL Server Database using VB.net ASP.net
Jan 31, 2013 11:45 AM|LINK
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.