Ranjitha M

Ranjitha M

  • NA
  • 21
  • 6.8k

Inserting data if i click on button

Jul 14 2017 6:32 AM
hi iam doing a project on asp.net .I am able to display data in my project but what i want to get is if i click on button named as "ADDQUEUEDETAILS" i should insert data into database,can anyone help me out .Iam uploading my code if there is any modifications in my code let me know.
 
This is my controller code
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MySql.Data.MySqlClient;
using System.Data;
using System.Data.SqlClient;
using Getqueuedetails.Models;
using Getqueuedetails.Controllers;
namespace GetData.Controllers
{
public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
[HttpGet]
public JsonResult GetData(string ReleaseId, string ReleaseName, string Scopelockdate, string ReleaseDate, string Segment, string Query, string Status)
{
List<Release> ObjClass = new List<Release>();
// MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=prefautomationdb;password=Password@2");
// con.Open();
// MySqlCommand cmd = new MySqlCommand();
// cmd.CommandText = "getReleaseDetails";
//cmd.CommandType = CommandType.StoredProcedure;
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=prefautomationdb;password=Password@2");
con.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM tfsperfqueue", con);
//MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
//DataSet ds = new DataSet();
//adp.Fill(ds);
//cmd.Dispose();
//con.Close();
//return Json(ObjClass, JsonRequestBehavior.AllowGet);
var model = new List<Release>();
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
var release1 = new Release();
release1.ReleaseId = Convert.ToInt32(rdr["Release_Id"]);
release1.ReleaseName = Convert.ToString(rdr["Release_Name"]);
release1.Scopelockdate = Convert.ToDateTime(rdr["Scope_lock_date"]);
release1.Releasedate = Convert.ToDateTime(rdr["Release_Date"]);
release1.Segment = Convert.ToString(rdr["Segment"]);
release1.Query = Convert.ToString(rdr["Query"]);
release1.Status = Convert.ToInt32(rdr["Status"]);
model.Add(release1);
}
return Json(model, JsonRequestBehavior.AllowGet);
}
[HttpGet]
public ActionResult Save(string ReleaseId, string ReleaseName, string Scopelockdate, string ReleaseDate, string Segment, string Query, string Stauts)
{
//int ReleaseId = Convert.ToInt32(Request.Form["ReleaseId"].ToString());
//string ReleaseName = Convert.ToString(Request.Form["ReleaseName"].ToString());
//DateTime Scopelockdate = Convert.ToDateTime(Request.Form["Scopelockdate"].ToString());
//DateTime ReleaseDate = Convert.ToDateTime(Request.Form["ReleaseDate"].ToString());
//string Segment = Convert.ToString(Request.Form["Segment"].ToString());
//string Query = Request.Form["Query"].ToString();
//int Stauts = Convert.ToInt16(Request.Form["Stauts"].ToString());
MySqlConnection con = new MySqlConnection("server=localhost;user id=root;database=prefautomationdb;password=Password@2");
con.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "sp_InsertReleasedetails";
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@ReleaseId", ReleaseId != null ? Convert.ToInt32(ReleaseId) : 0);
cmd.Parameters.AddWithValue("@ReleaseName", ReleaseName);
cmd.Parameters.AddWithValue("@Scopelockdate", Scopelockdate != null ? Convert.ToDateTime(Scopelockdate) : DateTime.MinValue);
cmd.Parameters.AddWithValue("@ReleaseDate", ReleaseDate != null ? Convert.ToDateTime(ReleaseDate) : DateTime.MinValue);
cmd.Parameters.AddWithValue("@Segment", Segment);
cmd.Parameters.AddWithValue("@Query", Query);
cmd.Parameters.AddWithValue("@Stauts", Stauts != null ? Convert.ToInt32(Stauts) : 0);
cmd.ExecuteNonQuery();
con.Close();
RedirectToAction("Index");
return View();
}
}
}
 
This is index.cshtml
 
 
@{
ViewBag.Title = "Index";
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
$.getJSON("GetData/Default1",
function (json) {
var tr;
//Append each row to html table
for (var i = 0; i < json.length; i++) {
tr = $('<tr/>');
tr.append("<td>" + json[i].ReleaseId + "</td>");
tr.append("<td>" + json[i].ReleaseName + "</td>");
tr.append("<td>" + json[i].Scopelockdate + "</td>");
tr.append("<td>" + json[i].Releasedate + "</td>");
tr.append("<td>" + json[i].Segment + "</td>");
tr.append("<td>" + json[i].Query + "</td>");
tr.append("<td>" + json[i].Status + "</td>");
$('table').append(tr);
}
});
$("#btnAddQueueDetails").click(function () {
// debugger;
var frmdata = {
// ReleaseId: $("#txtReleaseId").val(), //Reading text box values using Jquery
ReleaseName: $("#txtReleaseName").val(),
Scopelockdate: $("#txtScopelockdate").val(),
ReleaseDate: $("#txtReleaseDate").val(),
Segment: $("#txtSegment").val(),
Query: $("#txtQuery").val(),
Stauts: $("#txtStauts").val()
};
});

$.ajax(
{
type: "Get", //HTTP POST Method
url: '@Url.Action("GetData","Default1")', // Controller/View
//method: 'Post',
dataType: "json",
contentType: 'application/json',
success: function () {
alert("save successfully");
$.ajax(
{
type: "Get", //HTTP POST Method
url: '@Url.Action("Save","Default1")', // Controller/View
//method: 'Post',
dataType: "json",
contentType: 'application/json',
data: frmdata,
success: function () {
alert("save successfully");
//window.location.href = '/Default1/Insert';

//window.location.href = '/Default1/Insert';
},
error: function (error) {
alert(error)
},
});
}
});
});

</script>

<table class="table table-bordered table-condensed table-hover table-striped">
<thead>
<tr>
<th>ReleaseId</th>
<th>ReleaseName</th>
<th>Scopelockdate</th>
<th>Releasedate</th>
<th>Segment</th>
<th>Query</th>
<th>Stauts</th>

</tr>

</thead>

<tbody></tbody>
</table>
<input id="edit" type="submit" name="AddQueueDetails" value="AddQueueDetails" />
<br /><br />
<fieldset>
<div class="form-horizontal">
<div class="editor-label">
ReleaseName
</div>
<div class="editor-label">
<input type="text" id="txtReleaseName" />
</div>
<div class="editor-label">
Scopelockdate
</div>
<div class="editor-label">
<input type="text" id="txtScopelockdate" />
</div>
<div class="editor-label">
ReleaseDate
</div>
<div class="editor-label">
<input type="text" id="txtReleaseDate" />
</div>
<div class="editor-label">
Segment
</div>
<div class="editor-label">
<input type="text" id="txtSegment" />
</div>
<div class="editor-label">
Query
</div>
<div class="editor-label">
<input type="text" id="txtQuery" />
</div>
<input id="edit" type="submit" name="Save" value="Save" />
<br /><br />
</div>
</fieldset>
 

 

Answers (3)