In this articles of ASP.NET MVC we will learn about Insertion using sql linq query with jquery and ajax so let's get start.
Firstly make a connection with database and for connection,Open visual studio on the left hand side goto server explorer and make a connection on click this button:
After Click on this button you will find a dialogbox called Add Connection ,after appearing a dialogbox add a server name (to find a server name firstly open sql server management studio and when you open you will find a server name just copy server name and paste on Add Connection dialogbox under server name input field). In my case , my server name is NAVEED-PC.
After adding server name just click OK button ,then you will find a connection with your database at the left corner under server explorer.
After making connection , just right click on the connection which is marked with red colour and goto properties and once you clicked the properties option you will find its properties on the right hand corner so just copy the connection string
After done all this stuff then we will create controller so in my case i create a HomeController
HOME CONTROLLER:
public ActionResult AddPage()
{
ViewBag.userName = new SelectList(db.GEN_Users, "sysUserID", "Username");
return View();
}
public ActionResult AddRecord(int txtuserId, string txttaskDate, string txttaskAssignTo, string txttaskAssignFrom, string txttaskDescription,string imageUploading, string filenameVariable, string filetypeVariable)
{
var list = db.Database.SqlQuery<GEN_TaskMaster>("insert into GEN_TaskMaster(sysUserID,Task_Date,Task_Assign_To,Task_Assign_From,Task_Description)values('" + txtuserId + "','" + txttaskDate + "','" + txttaskAssignTo + "','" + txttaskAssignFrom + "','" + txttaskDescription + "')").ToList();
return Json("Successfully Inserted", JsonRequestBehavior.AllowGet);
}
Create a view called AddPage and write a code below:
@{
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Add Record</title>
<link href="~/css/StyleSheet1.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/sweetalert2/6.4.3/sweetalert2.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/sweetalert2/latest/sweetalert2.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
</head>
<body>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 column_size_changer">
<p class="text-right text-size-changer" style="color:black;">Home / Add</p>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 column_size_changer">
<p class="text-size-changer font-family-changer" style=""></p>
</div>
<div class="container">
<div id="loginbox" style="" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-xs-12 col-sm-offset-2 font-family-changer">
<div class="panel panel-info">
<div class="panel-heading text-size-changer table-background-color-changer">
<div class="panel-title table-thead-text-size-changer text-center text-color-changer ">Add Record</div>
</div>
<div style="" class="panel-body">
<div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>
<form id="loginform" class="form-horizontal" role="form" method="post" action="">
<input type="hidden" name="txttaskId" value="" class="form-control txttaskId" id="txttaskId" required />
<div class="">
<span>User ID</span>
</div>
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon table-background-color-changer text-color-changer"><i class="glyphicon glyphicon-lock"></i></span>
@Html.DropDownList("GEN_Users", (SelectList)ViewBag.userName, "Select One", new { @class = "form-control txtuserId", id = "txtuserId" })
</div>
<input type="hidden" name="txttaskDate" id="txttaskDate" value="" class="form-control txttaskDate " required />
<div class="">
<span>Task Assign To</span>
</div>
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon table-background-color-changer text-color-changer"><i class="glyphicon glyphicon-lock"></i></span>
<input type="text" name="txttaskAssignTo" value="" id="txttaskAssignTo" class="form-control txttaskAssignTo " required />
</div>
<div class="">
<span>Task Assign From</span>
</div>
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon table-background-color-changer text-color-changer"><i class="glyphicon glyphicon-lock"></i></span>
<input type="text" name="txttaskAssignFrom" value="" id="txttaskAssignFrom" class="form-control txttaskAssignFrom" required />
</div>
<div class="">
<span>Task Description</span>
</div>
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon table-background-color-changer text-color-changer"><i class="glyphicon glyphicon-lock"></i></span>
<textarea cols="2" rows="2" name="txttaskDescription" id="txttaskDescription" value="" class="form-control txttaskDescription" required></textarea>
</div>
<div class="">
<span>File Upload</span>
</div>
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon table-background-color-changer text-color-changer"><i class="fa fa-upload"></i></span>
<input type="file" name="fileUpload" onchange="encodeImageFileAsURL(this)" value="" id="fileUpload" class="form-control fileUpload" required />
</div>
<div style="margin-top:10px" class="form-group">
<div class="col-sm-12 controls">
<input type="hidden" name="hdId" value="" class="btn table-background-color-changer text-color-changer btnSubmit " />
<input type="button" value="Submit" id="submitBtn" class="btn table-background-color-changer text-color-changer btnSubmit" />
<input type="button" value="Cancel" id="cancelBtn" class="btn table-background-color-changer text-color-changer" />
</div>
</div>
<input type="hidden" id="hiddenImage"/>
<input type="hidden" id="filename"/>
<input type="hidden" id="filetype" />
</form>
</div>
</div>
</div>
</div>
<script>
$("#txttaskDate").datepicker();
$("#txttaskDate").datepicker("setDate", new Date());
var imageUploading = "";
var filenameVariable = "";
var filetypeVariable = "";
function encodeImageFileAsURL(element) {
var file = element.files[0];
filenameVariable = element.files[0].name;
filetypeVariable = element.files[0].type;
var reader = new FileReader();
reader.onloadend = function () {
imageUploading = reader.result;
var hiddenImage = $("#hiddenImage").val(imageUploading);
}
reader.readAsDataURL(file);
}
$("#submitBtn").click(function () {
var txtuserId = $("#txtuserId option:selected").val();
var txttaskDate = $("#txttaskDate").val();
var txttaskAssignTo = $("#txttaskAssignTo").val();
var txttaskAssignFrom = $("#txttaskAssignFrom").val();
var txttaskDescription = $("#txttaskDescription").val();
$.ajax({
url: "/Home/AddRecord",
type: "POST",
data: { txtuserId: txtuserId, txttaskDate: txttaskDate, txttaskAssignTo: txttaskAssignTo, txttaskAssignFrom: txttaskAssignFrom, txttaskDescription: txttaskDescription, imageUploading: imageUploading, filenameVariable: filenameVariable, filetypeVariable: filetypeVariable},
dataType:"JSON",
success: function (result) {
window.location.href = "/Home/Dashboard";
},
error: function (err) {
alert("Something went wrong");
}
});
});
$("#cancelBtn").click(function () {
window.location.href = "/Home/Dashboard";
});
</script>
</body>
</html>
I create a model called GEN_TaskMaster:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Kassim.Models
{
public class GEN_TaskMaster
{
public long sysTaskID { get; set; }
public long sysUserID { get; set; }
public DateTime Task_Date { get; set; }
public string Task_Assign_To { get; set; }
public string Task_Assign_From { get; set; }
public string Task_Description { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}
I have design my database you can take data field in database whatever you want.
Thank you for reading , if you have any query then leave a message.
:
Comments
Post a Comment