Solved

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

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

776 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