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 with Zero if ASP.Net GridView Column is Null or Empty using C# and VB.Net<p>Hey <a class="username" rel="akhter"> akhter</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;gvCustomers&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;InTime&#34; HeaderText=&#34;InTime&#34; /&gt; &lt;asp:BoundField DataField=&#34;OutTime&#34; HeaderText=&#34;OutTime&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Hours&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtHoures&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;Hours&#34;) %&gt;&#39;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&#34;Days&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID=&#34;txtDay&#34; runat=&#34;server&#34; Text=&#39;&lt;%#Eval(&#34;Days&#34;) %&gt;&#39;&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:Button Text=&#34;Update&#34; runat=&#34;server&#34; OnClick=&#34;Update&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM TestDemotest&#34;, con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); da.Fill(dt); gvCustomers.DataSource = dt; gvCustomers.DataBind(); } } } } } protected void Update(object sender, EventArgs e) { foreach (GridViewRow row in gvCustomers.Rows) { string name = row.Cells[0].Text; string inTime = row.Cells[1].Text; string OutTime = row.Cells[2].Text; TextBox txtHours = row.FindControl(&#34;txtHoures&#34;) as TextBox; TextBox txtDay = row.FindControl(&#34;txtDay&#34;) as TextBox; int hours = 0, day = 0; if (inTime != &#34;&amp;nbsp;&#34; &amp;&amp; OutTime != &#34;&amp;nbsp;&#34;) { hours = Convert.ToInt32(txtHours.Text); day = Convert.ToInt32(txtDay.Text); } else if (inTime == &#34;&amp;nbsp;&#34; || OutTime == &#34;&amp;nbsp;&#34;) { txtHours.Text = hours.ToString(); txtDay.Text = day.ToString(); } using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString)) { using (SqlCommand cmd = new SqlCommand(&#34;Update TestDemotest set Hours = @hours,Days=@days WHERE name = @name&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@hours&#34;, hours); cmd.Parameters.AddWithValue(&#34;@days&#34;, day); cmd.Parameters.AddWithValue(&#34;@name&#34;, name); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Update(ByVal sender As Object, ByVal e As EventArgs) For Each row As GridViewRow In gvCustomers.Rows Dim name As String = row.Cells(0).Text Dim inTime As String = row.Cells(1).Text Dim OutTime As String = row.Cells(2).Text Dim txtHours As TextBox = TryCast(row.FindControl(&#34;txtHoures&#34;), TextBox) Dim txtDay As TextBox = TryCast(row.FindControl(&#34;txtDay&#34;), TextBox) Dim hours As Integer = 0, day As Integer = 0 If inTime &lt;&gt; &#34;&amp;nbsp;&#34; AndAlso OutTime &lt;&gt; &#34;&amp;nbsp;&#34; Then hours = Convert.ToInt32(txtHours.Text) day = Convert.ToInt32(txtDay.Text) ElseIf inTime = &#34;&amp;nbsp;&#34; OrElse OutTime = &#34;&amp;nbsp;&#34; Then txtHours.Text = hours.ToString() txtDay.Text = day.ToString() End If Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString) Using cmd As SqlCommand = New SqlCommand(&#34;Update TestDemotest set Hours = @hours,Days=@days WHERE name = @name&#34;, con) cmd.Parameters.AddWithValue(&#34;@hours&#34;, hours) cmd.Parameters.AddWithValue(&#34;@days&#34;, day) cmd.Parameters.AddWithValue(&#34;@name&#34;, name) con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Using Next End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/jpbW83h.gif" alt="" width="468" height="178" /></p> <p>Updated Database Table.</p> <table border="1"> <tbody> <tr> <td><strong>name</strong></td> <td><strong>InTime</strong></td> <td><strong>OutTime</strong></td> <td><strong>Hours</strong></td> <td><strong>Days</strong></td> </tr> <tr> <td>Mudassar</td> <td>5/26/2018 9:22</td> <td>5/26/2018 21:22</td> <td>12</td> <td>1</td> </tr> <tr> <td>john</td> <td>5/26/2018 9:22</td> <td>&nbsp;</td> <td>0</td> <td>0</td> </tr> <tr> <td>robert</td> <td>&nbsp;</td> <td>5/26/2018 21:22</td> <td>0</td> <td>0</td> </tr> </tbody> </table>https://www.aspforums.net:443/Threads/192163/Update-Database-with-Zero-if-ASPNet-GridView-Column-is-Null-or-Empty-using-C-and-VBNet/https://www.aspforums.net:443/Threads/192163/Update-Database-with-Zero-if-ASPNet-GridView-Column-is-Null-or-Empty-using-C-and-VBNet/Tue, 01 Jan 2019 00:25:38 GMT