Wednesday, 23 July 2014

Light Box in Asp.net (Login Page) connecting to DB

<html>
<head>
<title>LIGHTBOX EXAMPLE</title>

<script>
 function Login() {
            var input = "Login" + "~" + $("input[username='username']").val() + "~" + $("input[password='password']").val();
            $.ajax({
                url: "Home.aspx",
                type: "GET",
                data: input,
                //contentType: "application/json; charset=utf-8",
                dataType: "text",
                success: function (data) {
                    if (data == 'Success..!') {
                        var url = 'Index.aspx';
                        $(location).attr('href', url);
                    }
                    else
                        alertify.alert("Sorry, Your login failed. Please re-enter Username and Password..!");
                },
                error: function (err) {
                    alertify.alert("Sorry, Your login failed. Please re-enter Username and Password.");
                }
            });

        }

</script>
<style type="text/css">
.black_overlay{
display:none;
positionabsolute;
top0%;
left0%;
width100%;
height100%;
background-color:black;
z-index:1001;
-moz-opacity0.8;
opacity:.80;
filteralpha(opacity=80);
}
.white_content {
display:none;
positionabsolute;
top25%;
left35%;
width35%;
padding0px;
border0px solid #a6c25c;
background-colorwhite;
z-index:1002;
overflowauto;
}
.headertext{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
color:#f19a19;
font-weight:bold;
}
.textfield
{
border:1px solid #a6c25c;
width:135px;
}
.button2
{
 background-color:#a6c25c;
 color:White;
 font-size:11px;
 font-weight:bold;
 border:1px solid #7f9db9;
 width:68px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<a href = "javascript:void(0)" onclick ="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">Click Here</a>
<div id="light" class="white_content">
 <table cellpadding=0 cellspacing=0 border=0 style="background-color:#a6c25c;" width="100%"><tr><tdheight="16px"><a href = "javascript:void(0)" onclick ="document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'"><imgsrc="close.gif" style="border :0px"  width="13px" align="right" height="13px"/></a></td></tr>
<tr><td style="padding-left:16px;padding-right:16px;padding-bottom:16px"> 
<table align="center"  border="0" cellpadding="0" cellspacing="0" style="background-color:#fff" width="100%">
<tr>
<td align="center" colspan="2" class="headertext" >Login Form </td>
</tr>
<tr><td>&nbsp;</td></tr>
<tr><td align="center">
<table><tr>
<td align="right">Username:</td><td><asp:TextBox ID="txtUser" runat="server" CssClass="textfield"></asp:TextBox>
</td>
</tr>
<tr><td height="10px"></td></tr>
<tr>
<td align="right">Password:</td>
<td><asp:TextBox ID="txtPwd" runat="server" CssClass="textfield"></asp:TextBox></td>
</tr>
<tr><td height="10px"></td></tr>
<tr>
<td> </td><td><asp:Button ID="Button1" runat="server" Text="Sign In" class="button2"/>
<asp:Button ID="Button2" runat="server" Text="Sign Up" class="button2" /></td>
</tr></table></td></tr>
<tr><td height="10px"></td></tr>
</table>
</td></tr>
</table>
<div align="center" class=" headertext">
<asp:Label ID="txtlbl" runat="server"  ></asp:Label></div>
</div>
<div id="fade" class="black_overlay"></div>
</form>
</body>
</html>


C# code::: in page load

string querystring1 = Request.Url.Query.Replace("?", "");
            string querystring = querystring1.Replace("%20", " ");

            if (querystring.IndexOf('~') > -1)
            {
                string key = querystring.Split(new char[] { '~' })[0];
                if (key == "Login")
                {
                    string[] searchstring = querystring.Split(new char[] { '~' });
                    //ArrayList logindata = GetContactCodeSearchCriteria(searchstring);
                    string responsestr = ValidLogin(searchstring);
                    if (responsestr == "Success")
                    {
                        HttpContext.Current.Response.Write("Success..!");
                        HttpContext.Current.Response.End();
                    }
                    else
                    {
                        HttpContext.Current.Response.Write("Error");
                        HttpContext.Current.Response.End();
                    }
                    HttpContext.Current.Response.Write(responsestr.ToString());
                    HttpContext.Current.Response.End();
                }

            }


method::

 private string ValidLogin(string[] searchstring)
    {
        String username = Server.UrlDecode(searchstring[1].ToString());
        String password = Server.UrlDecode(searchstring[2].ToString());

//pass theseparameters to DB get the return 'Succcess' or 'failure'

SqlConnection con = new SqlConnection("Database=Test;server=servername;uid=sa;password=*****");//pls change connection string here
        //select * from Login1 where Username='admin' and Password ='123'
        string str = "select * from Login1 where Username='"+username+"' and Password='"+password+"'";
        SqlDataAdapter da = new SqlDataAdapter(str,con);
        DataSet ds = new DataSet();
        da.Fill(ds, "Table1");
        if (ds.Tables[0].Rows.Count > 0)
        {
            return "Success";
        }
        else
        {
            return "failure";

        }

}