Search

Thursday, September 10, 2015
0 comments

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

7:50 AMThursday, September 10, 2015
Introduction
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  http://dotprogramming.blogspot.in/2013/06/how-to-make-database-table-in-visual.html

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]

AS
SELECT * from [Table] 
RETURN 0



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 ();
            con.Open();
            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;
                        break;
                        

                    }
                }
                if (flag ==true)
                    result.Text = "ALREADY EXISTS";
                else
                    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.
Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top