Asp.Net With SQL Connection
CREATE TABLE [dbo].[Register](
[id] [int] IDENTITY(1,1) NOT NULL,
[uname] [varchar](30) NULL,
[uemail] [varchar](30) NULL,
[uphone] [varchar](30) NULL,
[createddate] [date] NULL,
[upassword] [varchar](30) NULL,
PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
------------------------------
CREATE PROCEDURE [dbo].[Regitsration](
@Username nvarchar(50),
@Email varchar(30),
@password varchar(30))
As
Begin
Declare @Result varchar(20);
IF EXISTS(SELECT uname FROM Register WHERE uname = @Username)
Begin
Set @Result = 'Already Exist'
End
Else
Begin
Insert into Register(uname,uemail,createddate,upassword)
values(@Username,@Email,GETDATE(),@password);
Set @Result = 'Done'
End
select @Result as 'Result';
End
GO
-----------------------------------
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AspSql.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Register</title>
<link href="https://fonts.googleapis.com/css?family=Roboto:400,700" rel="stylesheet"></link>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></link>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"></link>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">
body{
color: #fff;
background: #63738a;
font-family: 'Roboto', sans-serif;
}
.form-control{
height: 40px;
box-shadow: none;
color: #969fa4;
}
.form-control:focus{
border-color: #5cb85c;
}
.form-control, .btn{
border-radius: 3px;
}
.signup-form{
width: 400px;
margin: 0 auto;
padding: 30px 0;
}
.signup-form h2{
color: #636363;
margin: 0 0 15px;
position: relative;
text-align: center;
}
.signup-form h2:before, .signup-form h2:after{
content: "";
height: 2px;
width: 30%;
background: #d4d4d4;
position: absolute;
top: 50%;
z-index: 2;
}
.signup-form h2:before{
left: 0;
}
.signup-form h2:after{
right: 0;
}
.signup-form .hint-text{
color: #999;
margin-bottom: 30px;
text-align: center;
}
.signup-form form{
color: #999;
border-radius: 3px;
margin-bottom: 15px;
background: #f2f3f7;
box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
padding: 30px;
}
.signup-form .form-group{
margin-bottom: 20px;
}
.signup-form input[type="checkbox"]{
margin-top: 3px;
}
.signup-form .btn{
font-size: 16px;
font-weight: bold;
min-width: 140px;
outline: none !important;
}
.signup-form .row div:first-child{
padding-right: 10px;
}
.signup-form .row div:last-child{
padding-left: 10px;
}
.signup-form a{
color: #fff;
text-decoration: underline;
}
.signup-form a:hover{
text-decoration: none;
}
.signup-form form a{
color: #5cb85c;
text-decoration: none;
}
.signup-form form a:hover{
text-decoration: underline;
}
</style>
</head>
<body style="background-color:white">
<form id="form1" runat="server">
<div class="signup-form">
<h2>Register</h2>
<p class="hint-text">Create your account. It's free and only takes a minute.</p>
<div class="form-group">
<div class="row">
<div class="col-xs-6"><input type="text" id="txtUserName" class="form-control" name="first_name" placeholder="First Name" required="required"></div>
<div class="col-xs-6"><input type="text" class="form-control" name="last_name" placeholder="Last Name" required="required"></div>
</div>
</div>
<div class="form-group">
<input id="txtemail" type="email" class="form-control" name="email" placeholder="Email" required="required">
</div>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Password" required="required">
</div>
<div class="form-group">
<input id="txtpwd" type="password" class="form-control" name="confirm_password" placeholder="Confirm Password" required="required">
</div>
<div class="form-group">
<label class="hint-text checkbox-inline"><input type="checkbox" required="required"> I accept the <a class="hint-text" href="#">Terms of Use</a> & <a class="hint-text" href="#">Privacy Policy</a></label>
</div>
<div class="form-group">
<button id="btnSubmit" type="submit" class="btn btn-success btn-lg btn-block">Register Now</button>
</div>
<div class="text-center">Already have an account? <a href="#">Sign in</a></div>
</div>
</form>
</body>
<script type="text/javascript">
$(document).ready(function () {
$("#btnSubmit").click(function (e) {
debugger;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url: "WebForm1.aspx/RegisterSubmit",
data: '{"uname":"' + $("#txtUserName").val() + '","email":"' + $("#txtUserName").val() + '","pwd":"' + $("#txtUserName").val() + '"}',
success: function (res) {
var rows = JSON.parse(res.d);
alert("Hi1");
},
error: function (res) {
alert("Error");
$("#divAjaxLoader").addClass("hide");
}
});
});
});
</script>
</html>
------------------------------------------
WebForm1.aspx.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
namespace AspSql
{
public partial class WebForm1 : System.Web.UI.Page
{
private static DataSet _dataSet = null;
private static SqlParameter[] _sqlParameter = null;
private static DBConnection _helper = null;
private static string _queryStatement = string.Empty;
private static string _jsonOutput = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
}
public WebForm1()
{
_helper = new DBConnection();
}
[WebMethod]
public static string RegisterSubmit(string uname, string email, string pwd)
{
_jsonOutput = string.Empty;
_queryStatement = "[DMS].[Regitsration]";
_sqlParameter = new SqlParameter[] {
new SqlParameter("@Username",uname),
new SqlParameter("@Email",email),
new SqlParameter("@password",pwd),
};
try
{
_dataSet = _helper.GetDataSet(_queryStatement, _sqlParameter);
if (_dataSet.Tables[0].Rows.Count > 0)
{
_jsonOutput = _helper.GetJsonDataset(_dataSet);
}
else
{
_jsonOutput = "";
}
}
catch (Exception ex)
{
_jsonOutput = "";
}
return _jsonOutput;
}
}
}
----------------------------------------------------
Dbconnection.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
namespace AspSql
{
public class DBConnection
{
private string _connectionString = ConfigurationManager.ConnectionStrings["RegisterConnection"].ConnectionString;
private SqlConnection _sqlConnection = null;
private SqlCommand _sqlCommand = null;
private SqlDataAdapter _sqlDataAdapter = null;
private DataSet _dataSet = null;
private DataTable _dataTable = null;
public DBConnection()
{
//
// TODO: Add constructor logic here
//
}
public SqlDataAdapter common(string queryStatement, SqlParameter[] sqlParams)
{
_sqlConnection = new SqlConnection(_connectionString);
_sqlCommand = new SqlCommand(queryStatement, _sqlConnection);
_sqlCommand.CommandType = CommandType.StoredProcedure;
_sqlCommand.Parameters.AddRange(sqlParams);
_sqlDataAdapter = new SqlDataAdapter(_sqlCommand);
return _sqlDataAdapter;
}
public DataSet GetDataSet(string queryStatement, SqlParameter[] sqlParams)
{
_dataSet = new DataSet();
try
{
_sqlConnection = new SqlConnection(_connectionString);
_sqlCommand = new SqlCommand(queryStatement, _sqlConnection);
_sqlCommand.CommandType = CommandType.StoredProcedure;
_sqlCommand.Parameters.AddRange(sqlParams);
_sqlDataAdapter = new SqlDataAdapter(_sqlCommand);
_sqlDataAdapter.Fill(_dataSet);
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString());
}
return _dataSet;
}
public DataTable GetDataTable(string queryStatement, SqlParameter[] sqlParams)
{
_dataTable = new DataTable();
try
{
_sqlDataAdapter = common(queryStatement, sqlParams);
_sqlDataAdapter.Fill(_dataTable);
}
catch (Exception ex)
{
}
return _dataTable;
}
public string GetJson(DataTable dt)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName.Trim(), dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
public string GetJsonDataset(DataSet ds)
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
ArrayList root = new ArrayList();
List<Dictionary<string, object>> table;
Dictionary<string, object> data;
foreach (DataTable dt in ds.Tables)
{
table = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
data = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
data.Add(col.ColumnName, dr[col]);
}
table.Add(data);
}
root.Add(table);
}
return serializer.Serialize(root);
}
}
}
Comments
Post a Comment