How can i check if a specific entry in a database table exist or not

Yahya

Member
Joined
Apr 5, 2011
Messages
7
Programming Experience
1-3
Hello there am working on ASP.NET project using ADO.NET i need to check if a student_id exist in the Student table or not, if it exist i need to make an action if not an error should be displayed, how can i do this? how can i check if it exist? hopefully someone will help me!
 
Check out the first code example:

Retrieving and Saving Data in Databases

You can use COUNT or equivalent instead of SUM.

thank you for the reply
the thread you supplied was very good, but

VB.NET:
[LEFT]
[LIST=1]
[*][FONT=Courier New]Using connection [COLOR=#FF8000]As[/COLOR] [COLOR=#FF8000]New[/COLOR] SqlConnection[COLOR=#000000]([/COLOR][COLOR=#808080]"connection string here"[/COLOR][COLOR=#000000])[/COLOR][/FONT]
[*][FONT=Courier New]    Using [COLOR=#0600FF]command[/COLOR] [COLOR=#FF8000]As[/COLOR] [COLOR=#FF8000]New[/COLOR] SqlCommand[COLOR=#000000]([/COLOR][COLOR=#808080]"SELECT SUM(Quantity) FROM StockItem"[/COLOR], _[/FONT]
[*][FONT=Courier New]                                    connection[COLOR=#000000])[/COLOR][/FONT]
[*][FONT=Courier New]        connection.[COLOR=#0600FF]Open[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][/FONT]
[*][FONT=Courier New] [/FONT]
[*][FONT=Courier New]        [COLOR=#0600FF]Dim[/COLOR] totalQuantity [COLOR=#FF8000]As[/COLOR] [COLOR=#FF0000]Double[/COLOR] = [COLOR=#0600FF]CDbl[/COLOR][COLOR=#000000]([/COLOR][COLOR=#0600FF]command[/COLOR].[COLOR=#0000FF]ExecuteScalar[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000])[/COLOR][/FONT]
[*][FONT=Courier New] [/FONT]
[*][FONT=Courier New]        [COLOR=#008080][I]'Use totalQuantity here.[/I][/COLOR][/FONT]
[*][FONT=Courier New]    [COLOR=#0600FF]End[/COLOR] Using[/FONT]
[*][FONT=Courier New][COLOR=#0600FF]End[/COLOR] Using [/FONT]
[/LIST]

[/LEFT]

can u explain more, i mean for example, i want the user to enter his/her ID in a textbox, if the ID exist in the Student table, i want the website to navigate to a specific page, what should i change in the above code?
 
You adapt that SQL code to use COUNT instead of SUM and to use your table and column names. If you want to filter the data being counted based on a condition then you add a WHERE clause. There are examples of WHERE clauses elsewhere in that post. If you want to insert a value into that WHERE clause then you use a parameter. Again, there are examples elsewhere in that post of using parameters. Make sure you follow the example(s) that use(s) AddWithValue. For more info on parameters, follow the Blog link in my signature and check out my post on the subject.
 
Back
Top