This problem is coming because your Dataset is not getting cleared. Always make a habbit to clear dataset before using.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim fl As Integer=0
Dim t1 As Integer = TextBox1.Text
Dim waterarr(200) As String
sqlconn.ConnectionString = "Data Source=ADMIN-PC;Initial Catalog=TABLE NAME;User Id=sa;Password=;MultipleActiveResultSets=True"
Try
sqlconn.Open()
Catch ex As Exception
MsgBox(ex.Message, "Connection is failed")
End Try
For i As Integer = 0 To t1 - 1
TABLEarr(i) = "table" & i.ToString
Next
Dim adapter1 As New SqlDataAdapter()
Dim ds As New DataSet()
fl = ComboBox1.SelectedIndex
ds.Clear()
Dim strCmd3 As String = "SELECT * FROM" + "[" + TABLEarr(fl) + "]"
sqlCmd3 = New SqlCommand(strCmd3, sqlconn)
adapter1.SelectCommand = sqlCmd3
adapter1.Fill(ds, TABLEarr(fl))
Dim DataGridView1 As New DataGridView
Dim x As Integer = 100, y As Integer = 200
DataGridView1.Location = New System.Drawing.Point(x, y)
DataGridView1.Width = 250
DataGridView1.Height = 260
DataGridView1.DataSource = ds.DefaultViewManager
DataGridView1.DataSource = ds.Tables(waterarr(fl))
DataGridView1.Visible = True
Me.Controls.Add(DataGridView1)
sqlconn.Close()
End Sub
Thanks
Ashish