-->

Wednesday, June 19, 2013

How to check data in database in ASP.NET

How to check data in database in ASP.NET

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 = true;

        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 = false;
                        break;
                        

                    }
                }
                if (flag == false)
                    result.Text = "ALREADY EXISTS";
                else
                    result.Text = "not existes";

                                
            }
        }

    }
}

Out put of the program
data exists
not exists


Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved