Mvc Registration page With user exist using Ajax method
Introduction :In mvc registration page with ajax, we are explain how to call ajax method to call the controller and how to bind data from controller methode to insert this data to database and how to bind these data in grid contol in mvc with ajax,jquery and sql database with simpler programing language
View : Insert.cshtml
<script>
function Data() {
var ct = document.getElementById('dp');
var Type = ct.options[ct.selectedIndex].text;
var Name = $("#Name").val();
var Comp = $("#Comp").val();
var Password = $("#Password").val();
$.ajax({
url: '@Url.Action("Insert")',
data: { Name: Name, Comp: Comp, Password: Password, Type: Type },
type: 'POST',
dataType: 'json',
success: function (result) {
if (result.status) {
alert(result.message);
window.location.href = result.Url;
}
else {
alert(result.message);
}
}
});
}
</script>
<fieldset>
<legend>TestModel</legend>
<div class="editor-label">
Name
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
</div>
<div class="editor-label">
Company
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Comp)
</div>
<div class="editor-label">
Type
</div>
<div class="editor-field">
<select class="form-control" id="dp">
<option>Admin</option>
<option>Client</option>
</select>
</div>
<div class="editor-label">
Password
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
</div>
<p>
<input type="button" value="Insert" onclick="Data();" />
</p>
</fieldset>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Controller : Insert Action
public ActionResult Insert(string Name,string Comp,string Password,string Type)
{
string reply = "";
bool blSucceeded = false;
DataTable dt = new DataTable();
SqlCommand cmd1 = new SqlCommand("SP_Emp",con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.Add("@Name", SqlDbType.VarChar);
cmd1.Parameters.Add("@Query", SqlDbType.VarChar);
cmd1.Parameters["@Query"].Value = "UCheck";
cmd1.Parameters["@Name"].Value = Name;
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd1;
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
blSucceeded = false;
reply = "User Exist";
}
else
{
con.Open();
SqlCommand cmd = new SqlCommand("SP_Emp", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Empid", SqlDbType.Int);
cmd.Parameters.Add("@Name", SqlDbType.VarChar);
cmd.Parameters.Add("@Comp", SqlDbType.VarChar);
cmd.Parameters.Add("@Type", SqlDbType.VarChar);
cmd.Parameters.Add("@Password", SqlDbType.VarChar);
cmd.Parameters.Add("@Query", SqlDbType.VarChar);
cmd.Parameters["@Query"].Value = "Insert";
cmd.Parameters["@Empid"].Value = 0;
cmd.Parameters["@Name"].Value = Name;
cmd.Parameters["@Comp"].Value = Comp;
cmd.Parameters["@Type"].Value = Type;
cmd.Parameters["@Password"].Value = Password;
cmd.ExecuteNonQuery();
con.Close();
Session["Name"] = Name;
Session["Type"] = Type;
blSucceeded = true;
reply = "User Added Successful";
}
return new JsonResult { Data = new { status = blSucceeded, Url="/Test/LoginSuccess", message = reply, JsonRequestBehavior.AllowGet } };
}
StoreProcedure :
ALTER PROCEDURE [dbo].[SP_Emp]
@Empid int=null,
@Name varchar(50)=null,
@Comp varchar(50)=null,
@Type varchar(50)=null,
@Password varchar(50)=null,
@Query varchar(50)
AS
if(@Query='Insert')
BEGIN
insert into tbl_user values(@Name,@Comp,@Type,@Password);
END
if(@Query='UCheck')
BEGIN
select Name from tbl_user where Name=@Name;
END
if(@Query='Login')
BEGIN
select Name,Empid,Type from tbl_user where Name=@Name and Password=@Password;
END
if(@Query='SLogin')
BEGIN
select Name,Empid,Password from tbl_user where Name=@Name;
END
if(@Query='profile')
BEGIN
SELECT @Type = Type FROM tbl_user WHERE Empid = @Empid
if(@Type='Admin')
select Name,Empid,Password,Type,Comp from tbl_user where Type='Client';
else
select Name,Empid,Password,Type,Comp from tbl_user where Empid=@Empid;
END
Last step : Try this code to create mvc web application and program to visual studio
Programing code : Hello friend if you want this programing code please comment below with your email. so we are sent to your email
See Other Tutorial :
* AngularJS CRUD Operation : Select Insert Edit Update and Delete using AngularJS in ASP.Net MVC
* AngularJS With ASP.NET MVC
* Convert Rows to columns using 'Pivot' in SQL Server
* Mvc Registration page With user exist using Ajax method
* MVC 4 How to Perform Insert Update Delete Edit Select Operation
* MVC4 Edit,update,Delete,cancel inside gridview using sql database
* MVC 4 Gridview To Display Data Using SQL Server Database With Simple code
* Login page in asp.net Mvc4 Web application
* Mvc4 How to bind Dropdown List using Sql Database
* Gridview find control in asp.net
Sign up here with your email
2 comments
Write commentsThanks for posting this artical......
ReplyHmm is anyone else encountering problems with the pictures on this blog loading?
ReplyI'm trying to find out if its a problem on my end
or if it's the blog. Any feedback would be greatly appreciated.
ConversionConversion EmoticonEmoticon