ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Update Database record on ASP.Net TextBox change inside GridView using jQuery AJAX, C# and VB.Net<p>Hi&nbsp;<a class="username" rel="Warren">Warren,</a></p> <p>It is not possible to check with your code. I have created small example, so that you can understand it.</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>I have made use of the following table <span style="text-decoration: underline;">Customers</span> with the schema as follows.</p> <p><img src="https://www.aspsnippets.com/Handlers/DownloadFile.ashx?File=f18ac914-bc9b-437a-88e2-bd640ce05282.png" alt="" width="384" height="137" /></p> <p>I have already inserted few records in the table.</p> <p><img src="https://www.aspsnippets.com/Handlers/DownloadFile.ashx?File=b736972b-595c-4656-ab75-976e054877c7.png" alt="" width="340" height="156" /></p> <p>You can download the database table SQL by clicking the download link below.</p> <p><a href="https://www.aspsnippets.com/DownloadFile.aspx?File=Customers_Table_AutoIncrement.sql">Download SQL file</a></p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;gvCustomer&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; DataKeyNames=&#34;CustomerId&#34; CssClass=&#34;table table-striped table-bordered table-hover&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CustomerId&#34; HeaderText=&#34;ID&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Name&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtName&#34; Text=&#39;&lt;%# Eval(&#34;Name&#34;) %&gt;&#39; CssClass=&#34;form-control Update&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Country&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtCountry&#34; Text=&#39;&lt;%# Eval(&#34;Country&#34;) %&gt;&#39; CssClass=&#34;form-control Update&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#39;.Update&#39;).on(&#39;change&#39;, function () { var id = $(this).closest(&#39;tr&#39;).find(&#39;td&#39;).eq(0).html(); var name = $(this).closest(&#39;tr&#39;).find(&#39;[id*=txtName]&#39;).val(); var country = $(this).closest(&#39;tr&#39;).find(&#39;[id*=txtCountry]&#39;).val(); $.ajax({ type: &#34;POST&#34;, url: &#34;Default.aspx/Update&#34;, data: &#34;{id:&#34; + id + &#34;,name:&#39;&#34; + name + &#34;&#39;,country:&#39;&#34; + country + &#34;&#39;}&#34;, contentType: &#34;application/json; charset=utf-8&#34;, dataType: &#34;json&#34;, success: function (r) { alert(&#34;Record updated successfully.&#34;); }, error: function (r) { alert(r.responsetext); } }); }); }); &lt;/script&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.Services;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Imports System.Web.Services</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { gvCustomer.DataSource = GetData(); gvCustomer.DataBind(); } } private DataTable GetData() { string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; string query = &#34;SELECT CustomerId,Name,Country FROM Customers&#34;; SqlCommand cmd = new SqlCommand(query); using (SqlConnection con = new SqlConnection(conString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); return dt; } } } } [WebMethod] public static void Update(int id, string name, string country) { string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; string query = &#34;UPDATE Customers SET Name = @Name,Country = @Country WHERE CustomerId = @Id&#34;; using (SqlConnection con = new SqlConnection(conString)) { SqlCommand cmd = new SqlCommand(query, con); cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue(&#34;@Name&#34;, name); cmd.Parameters.AddWithValue(&#34;@Country&#34;, country); cmd.Parameters.AddWithValue(&#34;@Id&#34;, id); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not IsPostBack Then gvCustomer.DataSource = GetData() gvCustomer.DataBind() End If End Sub Private Function GetData() As DataTable Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim query As String = &#34;SELECT CustomerId,Name,Country FROM Customers&#34; Dim cmd As SqlCommand = New SqlCommand(query) Using con As SqlConnection = New SqlConnection(conString) Using sda As SqlDataAdapter = New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Using dt As DataTable = New DataTable() sda.Fill(dt) Return dt End Using End Using End Using End Function &lt;WebMethod()&gt; Public Shared Sub Update(ByVal id As Integer, ByVal name As String, ByVal country As String) Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim query As String = &#34;UPDATE Customers SET Name = @Name,Country = @Country WHERE CustomerId = @Id&#34; Using con As SqlConnection = New SqlConnection(conString) Dim cmd As SqlCommand = New SqlCommand(query, con) cmd.CommandType = CommandType.Text cmd.Parameters.AddWithValue(&#34;@Name&#34;, name) cmd.Parameters.AddWithValue(&#34;@Country&#34;, country) cmd.Parameters.AddWithValue(&#34;@Id&#34;, id) con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshots</span></strong></p> <p><strong>The Form</strong></p> <p><strong><img src="https://i.imgur.com/R7cfj8W.gif" alt="" width="414" height="318" /></strong></p> <p><strong>Record After Update</strong></p> <p><strong><img src="https://i.imgur.com/RDxinr4.jpg" alt="" width="295" height="148" /></strong></p> <p><strong>Note:</strong> I have used inline query in the code for update. You need to change it with Stored Procedure.</p>https://www.aspforums.net:443/Threads/158556/Update-Database-record-on-ASPNet-TextBox-change-inside-GridView-using-jQuery-AJAX-C-and-VBNet/https://www.aspforums.net:443/Threads/158556/Update-Database-record-on-ASPNet-TextBox-change-inside-GridView-using-jQuery-AJAX-C-and-VBNet/Fri, 21 Dec 2018 02:26:26 GMT