Solved

VB.net - how to populate multiple textboxes with results from one field in query

Posted on 2013-01-28
2
354 Views
Last Modified: 2013-01-29
Hi.

I have a list of textboxes that I would like to be populated by a SQL query :

select ProductName from Products

This query will return 8 Product names.  I woul like to poopulate my textboxes with these product names.  Can anyone help?
0
Comment
Question by:Jasmin01
2 Comments
 
LVL 6

Accepted Solution

by:
esolve earned 250 total points
ID: 38830083
Add a panel to your web form:
 <asp:Panel ID="Panel1" runat="server"></asp:Panel>

Add code to populate textboxes:

Dim cn As New SqlConnection("SERVER=.;DATABASE=usersDB;UID=sa;PWD=;")
    Dim cm As New SqlCommand("SELECT TOP 10 First_Name FROM members", cn)
    Dim da As New SqlDataAdapter(cm)
    Dim ds As New DataSet()
    da.Fill(ds)

    For Each dr As DataRow In ds.Tables(0).Rows
      Dim txt As New TextBox()
      txt.Text = dr("first_name").ToString()
      Panel1.Controls.Add(txt)
    Next
0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 250 total points
ID: 38830183
1.If you  are waitning just 8 results from your Query
2.If your Textboxes are already there and their names are TextBox1,TextBox2 e.t.c
then you can do this
Imports System.Data.SqlClient
Public Class Form1
    Private ProductList As New List(Of String)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        Dim ProductConnection As New SqlConnection(YourProjectConnectionString)
        Dim ProductStr As String = "SELECT ProductName FROM Products"
        Dim ProductSql As New SqlCommand(ProductStr, ProductConnection)
        Dim ProductDa As New SqlDataAdapter(ProductSql)
        Dim ProductDs As New DataSet
        ProductDa.Fill(ProductDs)
        ProductList.Clear()
      
        For Each dr As DataRow In ProductDs.Tables(0).Rows
            ProductList.Add(dr.Item("ProductName").ToString)
        Next
  
       For i as Integer= 0 To ProductList.Count - 1
            Me.Controls.Find("TextBox" & i + 1, True)(0).Text = ProductList.Item(i)
        Next

    End Sub


End Class

Open in new window

If you dynamic populate the TxtBoxes using esolve's Solution just do not forget to adjust the Location of them to avoid overlap.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Closing all open child forms. 4 38
POWERSHELL: do / until, should continue even when there is an error 4 55
Close word object 13 38
Put window Form inside tab page 10 26
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now