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.Prevent Blank Windows Form DataGridView Row to insert in Database using C#<blockquote><cite>Kirron says:</cite> <div class="line number31 index30 alt2"><code class="csharp keyword">private</code> <code class="csharp keyword">void</code> <code class="csharp plain">btnClose_Click(</code><code class="csharp keyword">object</code> <code class="csharp plain">sender, EventArgs e)</code></div> <div class="line number32 index31 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">{</code></div> <div class="line number33 index32 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp keyword">for</code><code class="csharp plain">(</code><code class="csharp keyword">int</code> <code class="csharp plain">i = 0; i &lt; dataGridView1.Rows.Count; i++)</code></div> <div class="line number34 index33 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">{</code></div> <div class="line number35 index34 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp keyword">string</code> <code class="csharp plain">val = </code><code class="csharp keyword">this</code><code class="csharp plain">.dataGridView1.Rows[i].Cells[0].Value </code><code class="csharp keyword">as</code> <code class="csharp keyword">string</code><code class="csharp plain">;</code></div> <div class="line number36 index35 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp keyword">if</code> <code class="csharp plain">(</code><code class="csharp keyword">string</code><code class="csharp plain">.IsNullOrEmpty(val) == </code><code class="csharp keyword">false</code><code class="csharp plain">)</code></div> <div class="line number37 index36 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">{</code></div> <div class="line number38 index37 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">con = </code><code class="csharp keyword">new</code> <code class="csharp plain">SqlConnection(constring);</code></div> <div class="line number39 index38 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">con.Open();</code></div> <div class="line number40 index39 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">cmd = </code><code class="csharp keyword">new</code> <code class="csharp plain">SqlCommand();</code></div> <div class="line number41 index40 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">cmd.Connection = con;</code></div> <div class="line number42 index41 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">cmd.CommandText = </code><code class="csharp string">"insert into Barcode(PartNumber, Description, Quantity, Bdate, Location, Mrp, ParentModel) values('"</code> <code class="csharp plain">+ dataGridView1.Rows[i].Cells[0].Value + </code><code class="csharp string">"', '"</code> <code class="csharp plain">+ dataGridView1.Rows[i].Cells[1].Value + </code><code class="csharp string">"', '"</code> <code class="csharp plain">+ dataGridView1.Rows[i].Cells[2].Value + </code><code class="csharp string">"', '"</code> <code class="csharp plain">+ dataGridView1.Rows[i].Cells[3].Value + </code><code class="csharp string">"', '"</code> <code class="csharp plain">+ dataGridView1.Rows[i].Cells[4].Value + </code><code class="csharp string">"', '"</code> <code class="csharp plain">+ dataGridView1.Rows[i].Cells[5].Value + </code><code class="csharp string">"', '"</code> <code class="csharp plain">+ dataGridView1.Rows[i].Cells[6].Value + </code><code class="csharp string">"')"</code><code class="csharp plain">;</code></div> <div class="line number43 index42 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">cmd.ExecuteNonQuery();</code></div> <div class="line number44 index43 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">con.Close();</code></div> <div class="line number45 index44 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">}</code></div> <div class="line number46 index45 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp keyword">else</code></div> <div class="line number47 index46 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">{</code></div> <div class="line number48 index47 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp comments">//this.dataGridView1.Rows[i].Cells[0].Visible = false;</code></div> <div class="line number49 index48 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">MessageBox.Show(</code><code class="csharp string">"Some of the contents of this excel files are empty!"</code><code class="csharp plain">);</code></div> <div class="line number50 index49 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">}</code></div> <div class="line number51 index50 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">}</code></div> <div class="line number52 index51 alt1"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">MessageBox.Show(</code><code class="csharp string">"Part Details Uploaded Successfully!"</code><code class="csharp plain">);</code></div> <div class="line number53 index52 alt2"><code class="csharp spaces">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code class="csharp plain">}</code></div> </blockquote> <p>Replace the above code with below.</p> <pre class="brush: csharp">private void btnClose_Click(object sender, EventArgs e) { for (int i = 0; i &lt; dataGridView1.Rows.Count - 1; i++) { string val = this.dataGridView1.Rows[i].Cells[0].Value.ToString(); if (!string.IsNullOrEmpty(val)) { con = new SqlConnection(constring); con.Open(); cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = &#34;insert into Barcode(PartNumber, Description, Quantity, Bdate, Location, Mrp, ParentModel) values(&#39;&#34; + dataGridView1.Rows[i].Cells[0].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[1].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[2].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[3].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[4].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[5].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[6].Value + &#34;&#39;)&#34;; cmd.ExecuteNonQuery(); con.Close(); } else { //this.dataGridView1.Rows[i].Cells[0].Visible = false; MessageBox.Show(&#34;Some of the contents of this excel files are empty!&#34;); } } MessageBox.Show(&#34;Part Details Uploaded Successfully!&#34;); }</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/190124/Prevent-Blank-Windows-Form-DataGridView-Row-to-insert-in-Database-using-C/https://www.aspforums.net:443/Threads/190124/Prevent-Blank-Windows-Form-DataGridView-Row-to-insert-in-Database-using-C/Thu, 20 Dec 2018 05:28:51 GMTPrevent Blank Windows Form DataGridView Row to insert in Database using C#<p>Try this instead, it worked for me:</p> <pre class="brush: csharp"> private void btnClose_Click(object sender, EventArgs e) { for (int i = 0; i &lt; dataGridView1.Rows.Count - 1; i++) { string val = this.dataGridView1.Rows[i].Cells[0].Value.ToString(); { if (val == &#34;&#34;) { dataGridView1.Rows.RemoveAt(i); i--; } else { con = new SqlConnection(constring); con.Open(); cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = &#34;insert into Barcode(PartNumber, Description, Quantity, Bdate, Location, Mrp, ParentModel) values(&#39;&#34; + dataGridView1.Rows[i].Cells[0].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[1].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[2].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[3].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[4].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[5].Value + &#34;&#39;, &#39;&#34; + dataGridView1.Rows[i].Cells[6].Value + &#34;&#39;)&#34;; cmd.ExecuteNonQuery(); con.Close(); } } } </pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/190124/Prevent-Blank-Windows-Form-DataGridView-Row-to-insert-in-Database-using-C/https://www.aspforums.net:443/Threads/190124/Prevent-Blank-Windows-Form-DataGridView-Row-to-insert-in-Database-using-C/Thu, 20 Dec 2018 23:33:13 GMT