Solved

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

Posted on 2013-01-28
2
349 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

11 Experts available now in Live!

Get 1:1 Help Now