title: How to avoid duplication in a VB ASP GridView?
body: <p>I'm working on an asp.net VB web page. I have created a grid-view which will be adding data on a button click event, but in the 1st button click event, it is added two row to the grid-view and after the 1st button click situation is normal. How can I avoid this duplication in the 1st event?</p>
  
  <p>Here I attached the code and a screen print. I need to avoid the row with null values.</p>
  
  <pre><code>Imports System.Data
  Imports System.Data.SqlClient 
  Imports System.Collections
  Partial Class add_stock
  Inherits System.Web.UI.Page
  
  Private numOfColumns As Integer = 1
  Private ctr As Integer = 0
  Private tablepanel As Table = Nothing
  
  
  Dim row As New TableRow()
  Dim cell As New TableCell()
  Dim tb9 As New Button
  Dim panel2 As New Panel
  Dim DT As New DataTable
  
  
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
      If Not IsPostBack Then
          tablepanel = New Table()
          tablepanel.ID = "tableBuild"
          Session("table") = tablepanel
          ViewState("ctr") = ctr
          'BindGridview()
      End If
      ctr = CType(ViewState("ctr"), Int32)
      tablepanel = DirectCast(Session("table"), Table)
      Panel1.Controls.Add(tablepanel)
  
      DT.Columns.Add("ID")
      DT.Columns.Add("Name")
      DT.Columns(0).AutoIncrement = True
  
  End Sub
  
  Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim drCurrentRow As DataRow = Nothing
      If GridView1.Rows.Count &gt; 1 Then
          AddNewRow()
      ElseIf GridView1.Rows.Count = 1 Then
          AddNewRow()
      Else
          BindGridview()
          AddNewRow()
      End If
      'Place the table with textboxes on the page
      '-------- numOfColumns = 1
      '-------- GenerateTable(numOfColumns)
      'End of the table display with textboxes 
  
      ' Add row to grid view
  End Sub
  
  Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
      ClientScript.RegisterStartupScript(Me.GetType, "Button_Alert", "alert('Button clicked!');", True)
      numOfColumns = 1
      'Generate the Table based from the inputs
      GenerateTable(numOfColumns)
  End Sub
  Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
      GridView1.ShowFooter = True
  End Sub
  
  Protected Sub BindGridview()
      Dim dt As New DataTable()
      'dt.Columns.Add("rowid", GetType(Integer))
      dt.Columns.Add("Item Number", GetType(String))
      dt.Columns.Add("Item Category", GetType(String))
      dt.Columns.Add("Lead Time", GetType(String))
      dt.Columns.Add("Supplier", GetType(String))
      dt.Columns.Add("Item Quantity", GetType(String))
      dt.Columns.Add("Re-order Level", GetType(String))
      dt.Columns.Add("Unit Price", GetType(String))
      dt.Columns.Add("Retails Price", GetType(String))
      dt.Columns.Add("Item Details", GetType(String))
      Dim dr As DataRow = dt.NewRow()
      dr("Item Number") = String.Empty
      dr("Item Category") = String.Empty
      dr("Lead Time") = String.Empty
      dr("Supplier") = String.Empty
      dr("Item Quantity") = String.Empty
      dr("Re-order Level") = String.Empty
      dr("Unit Price") = String.Empty
      dr("Retails Price") = String.Empty
      dr("Item Details") = String.Empty
      dt.Rows.Add(dr)
      ViewState("Curtbl") = dt
      GridView1.DataSource = dt
      'GridView1.DataBind()
  End Sub
  
  Private Sub AddNewRow()
  
      Dim rowIndex As Integer
  
      If ViewState("Curtbl") IsNot Nothing Then
          Dim dt As DataTable = DirectCast(ViewState("Curtbl"), DataTable)
          Dim drCurrentRow As DataRow = Nothing
          If dt.Rows.Count &gt; 1 Then
              Dim rowcount As Integer = dt.Rows.Count
              drCurrentRow = dt.NewRow()
              '   drCurrentRow("rowid") = rowcount + 1
              dt.Rows(rowcount - 1)("Item Number") = TextBox21.Text
              dt.Rows(rowcount - 1)("Item Category") = DropDownList4.Text
              dt.Rows(rowcount - 1)("Lead Time") = TextBox19.Text
              dt.Rows(rowcount - 1)("Supplier") = DropDownList3.Text
              dt.Rows(rowcount - 1)("Item Quantity") = TextBox22.Text
              dt.Rows(rowcount - 1)("Re-order Level") = TextBox17.Text
              dt.Rows(rowcount - 1)("Unit Price") = TextBox23.Text
              dt.Rows(rowcount - 1)("Retails Price") = TextBox20.Text
              dt.Rows(rowcount - 1)("Item Details") = TextBox18.Text
              dt.Rows.Add()
              rowIndex += 1
              ViewState("Curtbl") = dt
              GridView1.DataSource = dt
              GridView1.DataBind()
              MsgBox("1st")
  
          ElseIf dt.Rows.Count = 1 Then
              Dim rowcount As Integer = dt.Rows.Count
              'drCurrentRow = dt.NewRow()
              '   drCurrentRow("rowid") = rowcount + 1
              MsgBox(rowcount + 1)
              dt.Rows(rowcount - 1)("Item Number") = TextBox21.Text
              dt.Rows(rowcount - 1)("Item Category") = DropDownList4.Text
              dt.Rows(rowcount - 1)("Lead Time") = TextBox19.Text
              dt.Rows(rowcount - 1)("Supplier") = DropDownList3.Text
              dt.Rows(rowcount - 1)("Item Quantity") = TextBox22.Text
              dt.Rows(rowcount - 1)("Re-order Level") = TextBox17.Text
              dt.Rows(rowcount - 1)("Unit Price") = TextBox23.Text
              dt.Rows(rowcount - 1)("Retails Price") = TextBox20.Text
              dt.Rows(rowcount - 1)("Item Details") = TextBox18.Text
              dt.Rows.Add()
              rowIndex += 1
  
              ViewState("Curtbl") = dt
              GridView1.DataSource = dt
              GridView1.DataBind()
              MsgBox("2nd")
          Else
              Dim rowcount As Integer = dt.Rows.Count
              drCurrentRow = dt.NewRow()
              '  drCurrentRow("rowid") = rowcount + 1
              dt.Rows(1)("Item Number") = TextBox21.Text
              dt.Rows(1)("Item Category") = DropDownList4.Text
              dt.Rows(1)("Lead Time") = TextBox19.Text
              dt.Rows(1)("Supplier") = DropDownList3.Text
              dt.Rows(1)("Item Quantity") = TextBox22.Text
              dt.Rows(1)("Re-order Level") = TextBox17.Text
              dt.Rows(1)("Unit Price") = TextBox23.Text
              dt.Rows(1)("Retails Price") = TextBox20.Text
              dt.Rows(1)("Item Details") = TextBox18.Text
              dt.Rows.Add()
              rowIndex += 1
              ViewState("Curtbl") = dt
              GridView1.DataSource = dt
              GridView1.DataBind()
              MsgBox("3rd")
          End If
      Else
          Response.Write("ViewState Value is Null")
      End If
  
  End Sub
  
  
  Protected Sub OnRowDeleting(sender As Object, e As GridViewDeleteEventArgs)
  
      Dim dt As DataTable = TryCast(ViewState("Curtbl"), DataTable)
      Dim RowToDelete As Integer = e.RowIndex
  
      If dt.Rows.Count &gt; 0 Then
          dt.Rows.RemoveAt(RowToDelete)
          dt.AcceptChanges()
          GridView1.DataSource = dt
      End If
      GridView1.DataBind()
  End Sub
  
  Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
      If e.Row.RowType = DataControlRowType.DataRow Then
          Dim item As String = e.Row.Cells(0).Text
          For Each button As Button In e.Row.Cells(2).Controls.OfType(Of Button)()
              If button.CommandName = "Delete" Then
                  button.Attributes("onclick") = "if(!confirm('Do you want to delete " + item + "?')){ return false; };"
              End If
          Next
      End If
  End Sub 
  End Class
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ZeWLC.png" rel="nofollow noreferrer">enter image description here</a></p>