Guest User

Untitled

a guest
Feb 25th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. <script type="text/javascript">
  2. $(document).ready(function () {
  3. $("#Button1").click(function () {
  4. $.ajax({
  5. type: 'POST',
  6. contentType: "application/json;charset=utf-8",
  7. url: 'Default2.aspx/InsertMethod',
  8. data: "{'username':'" + document.getElementById('txtusername').value + "','password':'" + document.getElementById("txtpassword").value + "'}",
  9. async: false,
  10. success: function (response) {
  11. $('#txtusername').val('');
  12. $('#txtpassword').val('');
  13. alert("record has been saved in database");
  14.  
  15. },
  16. error: function () {
  17. console.log('there is some error');
  18. }
  19.  
  20. });
  21.  
  22. });
  23.  
  24. });
  25.  
  26. [WebMethod]
  27. public static string InsertMethod(string username, string password)
  28. {
  29. SqlConnection con = new SqlConnection();
  30. con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
  31. con.Open();
  32. SqlCommand cmd = new SqlCommand("insert jquerydata values('" + username + "','" + password + "'", con);
  33. cmd.CommandType = CommandType.Text;
  34. cmd.ExecuteNonQuery();
  35. cmd.Dispose();
  36. con.Close();
  37. return "true";
  38.  
  39. }
  40.  
  41. SqlCommand cmd = new SqlCommand("insert jquerydata values('" + username + "','" + password + "'", con);
  42.  
  43. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default2.aspx.cs" Inherits="SO19542105.Default2" %>
  44.  
  45. <!DOCTYPE html>
  46.  
  47. <html xmlns="http://www.w3.org/1999/xhtml">
  48. <head runat="server">
  49. <title>SO19542105: how to save data in database using jquery</title>
  50. <script type="text/javascript" src="Scripts/jquery-1.8.2.js"></script>
  51. <script type="text/javascript">
  52. $(function () {
  53. $("#Button1").click(function () {
  54. $.ajax({
  55. type: "POST",
  56. contentType: "application/json;charset=utf-8",
  57. url: "Default2.aspx/InsertMethod",
  58. data: "{'username':'" + $("#txtusername").val() + "', 'password':'" + $("#txtpassword").val() + "'}",
  59. async: false,
  60. success: function (response) {
  61. $("#txtusername").val("");
  62. $("#txtpassword").val("");
  63. alert("record has been saved in database");
  64. },
  65. error: function () {
  66. console.log("there is some error");
  67. }
  68. });
  69. });
  70. });
  71. </script>
  72. </head>
  73. <body>
  74. <input type="text" id="txtusername" placeholder="username" />
  75. <input type="password" id="txtpassword" placeholder="password" />
  76. <input type="button" id="Button1" value="OK" />
  77. </body>
  78. </html>
  79.  
  80. using System;
  81. using System.Configuration;
  82. using System.Data.SqlClient;
  83. using System.Web.Services;
  84.  
  85. namespace SO19542105
  86. {
  87. public partial class Default2 : System.Web.UI.Page
  88. {
  89. protected void Page_Load(object sender, EventArgs e)
  90. {
  91.  
  92. }
  93.  
  94. [WebMethod]
  95. public static string InsertMethod(string username, string password)
  96. {
  97. using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
  98. using (var cmd = con.CreateCommand())
  99. {
  100. cmd.CommandText = "INSERT INTO jquerydata(username, password) VALUES(@username, @password)";
  101. cmd.CommandType = System.Data.CommandType.Text;
  102. cmd.Parameters.AddWithValue("@username", username);
  103. cmd.Parameters.AddWithValue("@password", password);
  104. con.Open();
  105. cmd.ExecuteNonQuery();
  106. con.Close();
  107. }
  108. return "true";
  109. }
  110. }
  111. }
  112.  
  113. Write Static method in C# i.e. in aspx.cs page to insert data
  114.  
  115. [WebMethod]
  116. public static void AddRecord(string username, string password)
  117. {
  118. //SIMPLE SQL QUERY INSERT INTO
  119. {
  120. using(var con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
  121. using (var cmd = con.CreateCommand())
  122. {
  123. cmd.CommandText = "INSERT INTO jquerydata(username, password) VALUES(@username, @password)";
  124. cmd.CommandType = System.Data.CommandType.Text;
  125. cmd.Parameters.AddWithValue("@username", username);
  126. cmd.Parameters.AddWithValue("@password", password);
  127. con.Open();
  128. cmd.ExecuteNonQuery();
  129. con.Close();
  130. }
  131. return "true";
  132. }
  133. }
  134. }
  135.  
  136.  
  137.  
  138. create a html to accept two fields and insert data on click of button
  139.  
  140. <script src="3.2.1/jquery.min.js" type="text/javascript"></script>
  141.  
  142. <script type="text/javascript">
  143.  
  144. $(document).ready(function () {
  145.  
  146. $("#Button1").on('click', function (e) {
  147.  
  148.  
  149. var userDetails = {
  150. username:$('#txtusername').val(),
  151. password: $('#txtpassword').val()
  152. };
  153.  
  154. $.ajax({
  155. type: "POST",
  156. url: "pagename.aspx/AddRecord",
  157. data: JSON.stringify(userDetails),
  158. contentType: "application/json; charset=utf-8",
  159. dataType: "json",
  160. success: OnSuccess,
  161. error: OnErrorCall
  162. });
  163.  
  164. function OnSuccess(response) {
  165. var result = response.d;
  166. if (result == "success") {
  167. $("#msg").html("New record addded successfully :)").css("color", "green");
  168. }
  169.  
  170. }
  171.  
  172. function OnErrorCall(response) {
  173. $("#msg").html("Error occurs :(").css("color", "red");
  174. }
  175.  
  176. });
  177.  
  178. });
  179.  
  180. </script>
  181.  
  182.  
  183.  
  184. <form id="form1">
  185. <div id="Tree">
  186. <input type="text" id="txtusername" placeholder="username" />
  187. <input type="password" id="txtpassword" placeholder="password" />
  188. <input type="button" id="Button1" value="OK" />
  189. </div>
  190. </form>
Add Comment
Please, Sign In to add comment