Showing posts with label Employee management system asp & sql server. Show all posts
Showing posts with label Employee management system asp & sql server. Show all posts

Sunday, August 12, 2018

how to create Username availability Using Ajax | Sradha Webcreations

how to create Check Username availability 
Using Ajax Sql Server + asp.net




<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>

<!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>Check Username availability Using Ajax</title>
    <style type="text/css">
    .waitingdiv {
    background-color: #F5F8FA;
    border: 1px solid #5A768E;
    color: #333333;
    font-size: 93%;
    margin-bottom: 1em;
    margin-top: 0.2em;
    padding: 8px 12px;
    width: 8.4em;
}
</style>
   
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="scriptmanager1" runat="server">
    </asp:ScriptManager>
    <script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
function BeginRequestHandler(sender, args) {
var state = document.getElementById('loadingdiv').style.display;
                if (state == 'block') {
                     document.getElementById('loadingdiv').style.display = 'none';
                } else {
                     document.getElementById('loadingdiv').style.display = 'block';
                }
args.get_postBackElement().disabled = true;
}
</script>
     <div>
   
     <asp:UpdatePanel ID="PnlUsrDetails" runat="server">
    <ContentTemplate>
    <table>
    <tr>
    <td>
    UserName:
    </td>
    <td>
       <asp:TextBox ID="txtUsername" runat="server" AutoPostBack="true" ontextchanged="txtUsername_TextChanged"/>
    </td>
    <td style="width: 64px">
      <div id="checkusername" runat="server"  Visible="false">
        <asp:Image ID="imgstatus" runat="server" Width="17px" Height="17px"/>
        <asp:Label ID="lblStatus" runat="server"></asp:Label>
    </div>
    </td>
    </tr>
    </table>
    <div class="waitingdiv" id="loadingdiv" style="display:none; margin-left:5.3em">
    <img src="LoadingImage.gif" alt="Loading" />Please wait...
    </div><table>
        <tr>
            <td>
                Password:
            </td>
            <td style="width: 188px">
                &nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
            <td style="width: 103px">
                <div id="Div1" runat="server"  Visible="false">
                    &nbsp;
                </div>
            </td>
        </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td style="width: 188px">
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                </td>
                <td style="width: 103px">
                    &nbsp;</td>
            </tr>
    </table>
    </ContentTemplate>
    </asp:UpdatePanel>
         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Create" /></div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:dbconnection %>"
        SelectCommand="SELECT * FROM [UserInformation]"
        ProviderName="<%$ ConnectionStrings:dbconnection.ProviderName %>"></asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
                ReadOnly="True" SortExpression="id" />
            <asp:BoundField DataField="username" HeaderText="username"
                SortExpression="username" />
            <asp:BoundField DataField="password" HeaderText="password"
                SortExpression="password" />
            <asp:BoundField DataField="imagename" HeaderText="imagename"
                SortExpression="imagename" />
            <asp:HyperLinkField DataNavigateUrlFields="imagepath" DataTextField="imagepath"
                HeaderText="View and Download" />
        </Columns>
    </asp:GridView>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
    </form>
</body>
</html>

Default.aspx.CS
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
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.IO ;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
   
    }
protected void txtUsername_TextChanged(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty(txtUsername.Text))
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
    con.Open();
SqlCommand cmd = new SqlCommand("select * from UserInformation where UserName=@Name", con);
cmd.Parameters.AddWithValue("@Name", txtUsername.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
checkusername.Visible = true;
imgstatus.ImageUrl = "NotAvailable.jpg";
lblStatus.Text = "UserName Already Taken";
System.Threading.Thread.Sleep(200);
}
else
{
checkusername.Visible = true;
imgstatus.ImageUrl = "Icon_Available.gif";
lblStatus.Text = "UserName Available";
System.Threading.Thread.Sleep(200);
}
}
else
{
checkusername.Visible = false;
}
}
    protected void Button1_Click(object sender, EventArgs e)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
     string insertSql = "INSERT INTO userinformation(username, password,imagename,imagepath) VALUES(@username, @password,@imagename,@imagepath)";

     String  filename;
        filename = Path.GetFileName( FileUpload1 .PostedFile.FileName );
               //'Save images into Images folder
     if  (filename.Length == 0)
        {
            filename = "No Picture ";
        }
        else
            {

            FileUpload1.SaveAs(Server.MapPath("Image/" + filename));

            }
       


     using (SqlConnection myConnection = new SqlConnection(connectionString))
     {
          myConnection.Open();
          SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
          myCommand.Parameters.AddWithValue("@username", txtUsername.Text.Trim ());
          myCommand.Parameters.AddWithValue("@password", TextBox1.Text.Trim ());
         myCommand.Parameters.AddWithValue ("@imagename", filename);
         myCommand.Parameters.AddWithValue("@imagepath", "Image/"+ filename);

          myCommand.ExecuteNonQuery();
          myConnection.Close();
     }

     // "Reset" the Subject and Body TextBoxes
txtUsername .Text = string.Empty;
TextBox1 .Text = string.Empty;
}
    }