Solved

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

Posted on 2013-01-28
2
391 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 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