Check, if value exists in Database ASP.NET C#

September 10, 2015 , 0 Comments

In this article i will show you availability of data in the database table, if exist then return true otherwise return false. In this article we will use linear search algorithm to search the data. So lets see the example of it.

follow some steps for checking data into database
Step 1: Create table in database

database table

table value

Step 2:  Take a web form name as "duplicatevalue.aspx"
Step 3:  Design a Webform

design form

Step 4: Make Connection string in web.config file

Step-5 : Create procedure for retrieving data 

CREATE PROCEDURE [dbo].[checkdata]

SELECT * from [Table] 

Step 6: Double click on "Check Existing" Button and make code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class duplicatevalue : System.Web.UI.Page
    SqlDataReader rd;

    protected void Page_Load(object sender, EventArgs e)

    protected void Button1_Click(object sender, EventArgs e)
        bool flag = false;

        using (SqlConnection con = new SqlConnection())
            con.ConnectionString =ConfigurationManager .ConnectionStrings ["ConnectionString"].ToString ();
            using (SqlCommand cmd = new SqlCommand())
                cmd.CommandText = "checkdata";
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                rd = cmd.ExecuteReader();
                while (rd.Read ())
                    if (rd["EmployeeId"].ToString().Equals(empid.Text))
                        flag = true;

                if (flag ==true)
                    result.Text = "ALREADY EXISTS";
                    result.Text = "not existes";



Out put of the program
data exists
not exists

The same article we can do with different method check this article in different style.

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google