title: Fetching data from database into text boxes
body: <p>I have designed a SQL Server database app in which I fetch data from database and then insert in text boxes. I use a function to fetch data in data table from database and then I populate textboxes. I have to use this coding again and again:</p>
  <pre class="lang-vb prettyprint-override"><code>If Dt.Rows.Count &gt; 0 Then
      
      TxtCust_Id.Text = Dt.Rows(0).Item(0)
      TxtCust_City.Text = Dt.Rows(0).Item(1)
      TxtCust_Area.Text = Dt.Rows(0).Item(2)
      
  Else
      
      TxtCust_Id.Text = String.Empty
      TxtCust_City.Text = String.Empty
      TxtCust_Area.Text = String.Empty
  End if 
  </code></pre>
  <p>Text boxes names changes according to query tables. My question is. Is it possible to make a function or procedure to populate data in text boxes from datatable using loops or any other method?  Thanks in advance.</p>
  <pre class="lang-vb prettyprint-override"><code>Shared Function ExecuteSelectDt(ByVal SelectCommand As String) As DataTable
      
      Cmd = New SqlClient.SqlCommand
      Sda = New SqlDataAdapter
      ' Dt = New DataTable
  
      Try
          DBConnection()    ' Database connection details
      
          Sda = New SqlDataAdapter(SelectCommand, Con)
          Dim dt2 As New DataTable
          Sda.Fill(dt2)
          CloseConnection()
          Return dt2
      Catch ex As Exception
          CloseConnection()
          MsgBox(ex.Message)
          Return dt
      End Try
  End Function
      
  Private Sub TxtCust_Name_Leave(sender As Object, e As EventArgs) Handles TxtCust_Name.Leave
      
      SQuery = &quot;select Cust_Id, Cust_City, Cust_Area from TBLCustommers where Cust_Name= '&quot; &amp; TxtCust_Name.Text &amp; &quot;'&quot;
      
      Dt = Nothing
      Dt = BM_Class_Liberary.SQLSereverDB.ExecuteSelectDt(SQuery)
      
      If Dt.Rows.Count &gt; 0 Then
      
          TxtCust_Id.Text = Dt.Rows(0).Item(0)
          TxtCust_City.Text = Dt.Rows(0).Item(1)
          TxtCust_Area.Text = Dt.Rows(0).Item(2)
      
      Else
      
          TxtCust_Id.Text = String.Empty
          TxtCust_City.Text = String.Empty
          TxtCust_Area.Text = String.Empty
      End if 
      
  End Sub
  </code></pre>