looping thru controls

M

Michael Beck

I am new to ASP.net. All my work so far has been in VB ans Access.

I want to dynamically popluate a list box with four columns of data. Only
one column is the value. This is what I have tried so far. How can I get
all four columns visible in the list box.

cSQL = "Select CustNum, Name, Address, City from tblCust where
Substring(Phone,4,7) = '" & txtInput.Text & "'"

Dim cmdList As New
SqlClient.SqlCommand(cSQL, conn)
Dim rs As SqlClient.SqlDataReader
Dim dtblNames As New DataTable
Dim drowNew As DataRow
rs = cmdList.ExecuteReader
dtblNames.Columns.Add(New DataColumn("CustID", GetType(String)))
dtblNames.Columns.Add(New DataColumn("CustName", GetType(String)))
dblNames.Columns.Add(New ataColumn("Address", GetType(String)))
dtblNames.Columns.Add(New DataColumn("City", getType(String)))

While rs.Read()
drowNew = dtblNames.NewRow()
drowNew("CustID") = rs(0)
drowNew("CustName") = rs(1)
drowNew("Address") = rs(2)
drowNew("City") = rs(3)
dtblNames.Rows.Add(drowNew)
End While

' I want the datatextfield to be all four values in four columns.
' How can I do it?
lstNames.DataTextField = "CustName"
lstNames.DataValueField = "CustID"
lstNames.DataBind()
 
C

Cor Ligthert

Michael,

You do it a little bit to difficult for yourself, I thought that it should
work as you wrote it, however much easier is. (written in this message not
checked so watch typos)
\\\
cSQL = "Select CustNum, Name, Address, City from tblCust where
Substring(Phone,4,7) = '" & txtInput.Text & "'"
Dim cmdList As New
dim da as new SqlClinet.SqlDataadapter(sSQL, conn)
Dim dtblNames As New DataTable
lstNames.DataTextField = "CustName"
lstNames.DataValueField = "CustID"
lstNames.DataBind()
///

However with the listbox you cannot get the 4 columns, therefore they made
the datagrid or the datalist, for which you do not have to set the
datatextfield and datavaluefield.

However you have to configure that using the designer (or write a lot in
HTML)

I hope this helps?

Cor
 
C

Cor Ligthert

brr..............
I deleted to much and forgot something to write
\\\
cSQL = "Select CustNum, Name, Address, City from tblCust where
Substring(Phone,4,7) = '" & txtInput.Text & "'"
Dim cmdList As New
dim da as new SqlClinet.SqlDataadapter(sSQL, conn)
Dim dtblNames As New DataTable
da.fill(dtblNames)
lstNames.DataSource = dtblNames
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top