Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to sort a list of objects using an item in an enclosed list 5 50
get row value in vb.net 4 18
location of a form 2 25
Open a link in vb.net 2 16
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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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