Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone,,
i have a doubt,,,

how to check whether given value present in sqldatabase? using asp.net!!!!

can any one can help
Posted
Updated 6-Jun-11 3:23am
v2
Comments
Ciumac Sergiu 6-Jun-11 9:22am    
The solution is simple, try selecting that value from the database, if the result is null, it is absent, and otherwise.
Ra-one 6-Jun-11 9:24am    
true...... try the same, Read the basics of ADO.Net

true...... try the same, Read the basics of ADO.Net
 
Share this answer
 
try

SqlCommand cmd = new SqlCommand("Your Command using where clause","your Connection");
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read()) // if reading 
            {
// values is present 
            }
           else { // not present}
 
Share this answer
 
For SQL Tutorial[^], nice and simple place to start on SQL statements.

For checking is a value is present in a table. The simplest way is
C#
// Create database connection
SqlConnection con = new SqlConnection(<Connection string goes where>);
con.Open(); // Open

// Check for value
SqlCommand cmd = new SqlCommand("SELECT id FROM tbl WHERE name='Kim'", con);

// Get value
object value = cmd.ExecuteScalar();

// Close database connection
con.Close();

// Check for value
if (value == null)
  Console.WriteLine("Value not in table");
else
  Console.WriteLine("Value found at id: " + value);
 
Share this answer
 
VB
Dim con As New SqlConnection("Your Connection String")
       If con.State = ConnectionState.Closed Then
           con.Open()
       End If
       Dim cmd As SqlCommand = New SqlCommand("Select * from YourTableName Where YourColumnName='" & YourValue & "'")
       Dim rd As SqlDataReader = cmd.ExecuteReader()
       If rd.Read = True Then
           'code here when value exist
           Response.Write("Your value exists in table")
       Else
           Response.Write("Your value does not exist..")
       End If
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900