.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Check Data dosenot exist in database

Posted By:satyapriyanayak       Posted Date: January 13, 2015    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

In this Code Snippets we will know data dose not exist in database.
 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Check_Data_dosenot_exist_database._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btn_check" runat="server" Text="Check Data dosenot exist database"
        BackColor="#FF99FF" Font-Bold="True" Width="329px" onclick="btn_check_Click"
         /><br />
    <asp:Label ID="Labelcheck"  Text="Please enter any name to be verified from the database" runat="server" BackColor="#FFFF99"
        Width="197px" ForeColor="#FF3300"></asp:Label>
        <asp:TextBox ID="txtUserName" runat="server" Width="197px"></asp:TextBox>
    
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
        ControlToValidate="txtUserName" ErrorMessage="*Name Required"></asp:RequiredFieldValidator>
    
    <br />
    <asp:Label ID="lblMessage" runat="server" BackColor="#FF3300"
        ForeColor="Black"></asp:Label>
    </div>
    </form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Check_Data_dosenot_exist_database
{
    public partial class _Default : System.Web.UI.Page
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlCommand com;
        string str;

        protected void btn_check_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connStr);
            con.Open();
            str = "select count(*)from employee where ename='" + txtUserName.Text + "'";
            com = new SqlCommand(str, con);
            int count = Convert.ToInt32(com.ExecuteScalar());
            if (count > 0)
            {
                lblMessage.Text = "username exist";
            }
            else
            {
                lblMessage.Text = "username doesnot exist";
            }
        }  
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend