title: How to declare a connection object on a class level connection not in local scope with VB.NET?
body: <p>I got the error:</p>
  
  <blockquote>
    <p>Fill: SelectCommand.Connection property has not been initialized.</p>
  </blockquote>
  
  <p>I think this is because I declare my data set not inside the <code>private sub</code>  but in the <code>public class</code>.....
  but I need to use the data set in more than once in different <code>private sub</code> during the program....how should I define it ?
  many thanks.</p>
  
  <pre><code>Imports System.Data
  Imports System.Data.SqlClient
  Public Class Form1
  Dim con As SqlConnection
  Dim strsql As String
  Dim da As New SqlDataAdapter(strsql, con)
  Dim ds As New DataSet()
  Dim strcon As String
  Dim newmode As Boolean
  Dim newrow As DataRow
  Dim cb As SqlCommandBuilder
  
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      newmode = False
      'Dim con As SqlConnection
      'Dim strsql As String
      con = New SqlConnection("initial catalog=test;data source=nazi;integrated security=sspi;")
      strsql = "select*from table_1"
      con.Open()
      'Dim da As New SqlDataAdapter(strsql, con)
      'Dim ds As New DataSet()
  
      da.Fill(ds, "dd")
  
      TextBox1.DataBindings.Add(New Binding("text", ds, "dd.tel"))
      TextBox2.DataBindings.Add(New Binding("text", ds, "dd.nam"))
      TextBox3.DataBindings.Add(New Binding("text", ds, "dd.address"))
      da.update(ds, "dd")
      con.Close()
  End Sub
  
  Private Sub empty()
      textrecord.text = ""
      TextBox1.text = ""
      TextBox2.text = ""
      TextBox3.text = ""
      TextBox4.text = ""
  End Sub
  
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      newrow = ds.Tables("dd").NewRow
      newmode = True
      textrecord.BackColor = Color.Red
      textrecord.Text = "new record"
      MsgBox("enter new record and press save")
      Call empty()
  End Sub
  
  End Class
  </code></pre>