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 status based on condition in ASP.Net using C# and VB.Net<p>Hi&nbsp;chetan,</p> <p>You need to first check the status. If the status is not Secondary then update with&nbsp;Secondary.</p> <p>Check the below example. You need to change where condition&nbsp;in the query for update the particular record as per your requirement.</p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtId&#34; /&gt; &lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlVisitors&#34;&gt; &lt;asp:ListItem Values=&#34;&#34; Text=&#34;Select&#34; /&gt; &lt;asp:ListItem Values=&#34;Misc&#34; Text=&#34;Misc&#34; /&gt; &lt;asp:ListItem Values=&#34;Secondary&#34; Text=&#34;Secondary&#34; /&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button Text=&#34;Save&#34; OnClick=&#34;Save&#34; runat=&#34;server&#34; /&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.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Configuration Imports System.Data.SqlClient</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Save(object sender, EventArgs e) { if (GetStatus(Convert.ToInt32(txtId.Text.Trim())).ToLower() != &#34;secondary&#34;) { using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString)) { using (SqlCommand cmd = new SqlCommand(&#34;UPDATE cmp SET cmp.status= @Status WHERE Id = @Id&#34;, con)) { con.Open(); cmd.Parameters.AddWithValue(&#34;@Id&#34;, txtId.Text.Trim()); cmd.Parameters.AddWithValue(&#34;@Status&#34;, ddlVisitors.SelectedItem.Text.Trim()); cmd.ExecuteNonQuery(); con.Close(); } } } } private string GetStatus(int id) { string status = &#34;&#34;; using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT Status FROM cmp WHERE Id = @Id&#34;, con)) { con.Open(); cmd.Parameters.AddWithValue(&#34;@Id&#34;, id); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read()) { status = dr[&#34;Status&#34;].ToString(); } con.Close(); } } return status; }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs) If GetStatus(Convert.ToInt32(txtId.Text.Trim())).ToLower() &lt;&gt; &#34;secondary&#34; Then Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString) Using cmd As SqlCommand = New SqlCommand(&#34;UPDATE cmp SET cmp.status= @Status WHERE Id = @Id&#34;, con) con.Open() cmd.Parameters.AddWithValue(&#34;@Id&#34;, txtId.Text.Trim()) cmd.Parameters.AddWithValue(&#34;@Status&#34;, ddlVisitors.SelectedItem.Text.Trim()) cmd.ExecuteNonQuery() con.Close() End Using End Using End If End Sub Private Function GetStatus(ByVal id As Integer) As String Dim status As String = &#34;&#34; Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT Status FROM cmp WHERE Id = @Id&#34;, con) con.Open() cmd.Parameters.AddWithValue(&#34;@Id&#34;, id) Dim dr As SqlDataReader = cmd.ExecuteReader() If dr.Read() Then status = dr(&#34;Status&#34;).ToString() End If con.Close() End Using End Using Return status End Function</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/314419/Update-status-based-on-condition-in-ASPNet-using-C-and-VBNet/https://www.aspforums.net:443/Threads/314419/Update-status-based-on-condition-in-ASPNet-using-C-and-VBNet/Mon, 28 Jan 2019 03:43:33 GMT