Friday, 23 December 2016

Checking Duplicate entry while insert in Database And Entire Gridview rows :  

Aspx Page: 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="samp.aspx.cs" Inherits="samp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            height: 23px;
        }
        .auto-style3 {
            height: 23px;
            width: 161px;
            text-align: right;
        }
        .btn {
  background: #3498db;
  background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
  background-image: -moz-linear-gradient(top, #3498db, #2980b9);
  background-image: -ms-linear-gradient(top, #3498db, #2980b9);
  background-image: -o-linear-gradient(top, #3498db, #2980b9);
  background-image: linear-gradient(to bottom, #3498db, #2980b9);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  padding: 10px 20px 10px 20px;
  text-decoration: none;
}

.btn:hover {
  background: #3cb0fd;
  background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -moz-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -ms-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -o-linear-gradient(top, #3cb0fd, #3498db);
  background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
  text-decoration: none;
}
.enjoy-css {
  display: inline-block;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  padding: 10px 20px;
  border: 1px solid #b7b7b7;
  -webkit-border-radius: 3px;
  border-radius: 3px;
  font: normal 20px/normal Verdana, Geneva, sans-serif;
  color: rgba(0,142,198,1);
  -o-text-overflow: clip;
  text-overflow: clip;
  background: rgba(252,252,252,1);
  -webkit-box-shadow: 2px 2px 2px 0 rgba(0,0,0,0.2) inset;
  box-shadow: 2px 2px 2px 0 rgba(0,0,0,0.2) inset;
  text-shadow: 1px 1px 0 rgba(255,255,255,0.66) ;
  -webkit-transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1);
  -moz-transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1);
  -o-transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1);
  transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1);
}
        .auto-style9 {
            height: 57px;
            width: 161px;
            text-align: right;
            font-family: "Lucida Sans", "Lucida Sans Regular", "Lucida Grande", "Lucida Sans Unicode", Geneva, Verdana, sans-serif;
            color: #000080;
        }
        .auto-style11 {
            height: 57px;
        }
        .auto-style15 {
            height: 49px;
        }
        .auto-style16 {
            height: 23px;
            width: 366px;
        }
        .auto-style17 {
            height: 57px;
            width: 366px;
        }
        .auto-style18 {
            height: 49px;
            width: 366px;
        }
        .auto-style19 {
            width: 366px;
        }
        .auto-style20 {
            height: 23px;
            width: 161px;
        }
        .auto-style21 {
            height: 57px;
            width: 161px;
        }
        .auto-style22 {
            height: 49px;
            width: 161px;
        }
        .auto-style23 {
            width: 161px;
        }

        .auto-style24 {
            color: #CC9900;
        }

    </style>
</head>
<body>
    <form id="form1" runat="server">
   <center>

        <div>
            <h1 class="auto-style24">
               Insert Data And Check Duplicate In Both Database And Gridview
            </h1>
        </div>
    </center>

    <div>
        <table style="width: 100%;">
            <tr>
                <td class="auto-style16"></td>
                <td class="auto-style3"></td>
                <td class="auto-style20"></td>
                <td class="auto-style1"></td>
            </tr>
            <tr>
                <td class="auto-style17"></td>
                <td class="auto-style9" >ID</td>
                <td class="auto-style21">
                    <asp:TextBox ID="TextBox1" runat="server" Height="23px" Width="224px" CssClass="enjoy-css"></asp:TextBox>
                </td>
                <td class="auto-style11"></td>
            </tr>
            <tr>
                <td class="auto-style18"></td>
                <td class="auto-style9">NAME</td>
                <td class="auto-style22">
                    <asp:TextBox ID="TextBox2" runat="server" Height="23px" Width="224px" CssClass="enjoy-css"></asp:TextBox>
                </td>
                <td class="auto-style15"></td>
            </tr>
            <tr>
                <td class="auto-style16">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style20">
                    &nbsp;</td>
                <td class="auto-style1">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style16"></td>
                <td class="auto-style3">
                   
                </td>
                <td class="auto-style20">
                    
                </td>
                <td class="auto-style1">
                    </td>
            </tr>
            <tr>
                <td class="auto-style19">&nbsp;</td>
                <td class="auto-style23">&nbsp;</td>
                <td class="auto-style23">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
    </div>
        <center>
        <div>
             <asp:Button ID="Button1" runat="server" Text=" Insert & Check Database" CssClass="btn"  OnClick="Button1_Click" />&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" CssClass="btn" Text=" Insert & Check Gridview" />
        </div>
            </center>
        <Center>

        <div>

            &nbsp;

            <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="389px">
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>
        </div>
                    </Center>

    </form>
</body>
</html>


C# Page : 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text;
using System.Collections.Specialized;
using System.Web.Services;

public partial class samp : System.Web.UI.Page
{
    MySqlConnection con1 = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
                    con1.Open();
                    MySqlCommand cmd= new MySqlCommand("Select count(*) from sample where id='"+TextBox1.Text+"' and name='"+TextBox2.Text+"'", con1);
                    int count = Convert.ToInt32(cmd.ExecuteScalar());
                    if (count == 0)
                    {

                        MySqlCommand cmdl = new MySqlCommand("insert into sample(id,name)values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con1);
                        cmdl.ExecuteNonQuery();
                        this.bind();
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Inserted Sucessfully')", true);
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        con1.Close();
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Already Exits.')", true);
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        con1.Close();
                    }

    }
    protected void bind()
    {
  
        MySqlCommand cmd = new MySqlCommand("select upper(id) as 'ID' ,upper(name) as 'NAME' from sample", con1);
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();


    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        bool entryFound = false;
        foreach (GridViewRow row in GridView1.Rows)
        {

            string val4 = row.Cells[0].Text;
       
        

            if (val4 != null && val4.ToString() ==TextBox1.Text)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Already Exits !')", true);
                entryFound = true;
                break;
            }

        }

        if (!entryFound)
        { 
                        con1.Open();
                        MySqlCommand cmdl = new MySqlCommand("insert into sample(id,name)values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con1);
                        cmdl.ExecuteNonQuery();
                        this.bind();
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Inserted Sucessfully')", true);
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        con1.Close();
        }
         else
            {
                 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Already Exits.')", true);
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        con1.Close();
            }
        }

    }



Screen Shots : 


 Showing Error message :







Download Source File with Table Structure :


     

How to create a simple Hello World website in ASP.NET MVC using Razor Syntax: (Note: I am using Visual Studio 2012 ) Step 1: ...