Showing posts with label column. Show all posts
Showing posts with label column. Show all posts

Sunday, March 11, 2012

Database results stored in a Array

How would I go about storing (1 column) database results in a Array?

And then how would I go about extracting the elements from the Array?What, you mean other than by loading it in a recordset/dataset and then looping through the data populating an array? I presume you wanna do this in an application, not SQL Server?

Because T-SQL does not support array structures as far as I know (unless you count the table variable as an array).

Some more info, please. :-)|||My problems is that I am trying to load e-mail values (from a database) into a listbox. The problem I'm facing is that when the e-mails are loaded into the listbox right now, they do not have index values. I was thinking of storing these results in a array so I could have an index and value together e.g. ArrayName( "1", "name@.domain.com").

If there would be an easier way to do this, then I'd like to hear about it. Eventually, I want to populate listbox1 with all the e-mails from the database and then use an "Add" button to insert the highlighted e-mail into listbox2. From there, I would have my chosen e-mails in listbox2 that would get an e-mail from me.

Would I still use a recordset/dataset? If so, how would I add indexes to make each value unique? Or would I have to convert the recordset/dataset into ListItems so that they would have indexes and values together?|||


Dim conPubs As SqlConnection
Dim cmdSelectAuthors As SqlCommand
Dim dtrAuthors As SqlDataReader
conPubs = New SqlConnection(ConfigurationSettings.appSettings("STDConnect"))
conPubs.Open()
cmdSelectAuthors = New SqlCommand("Select * from Email", conPubs)
dtrAuthors = cmdSelectAuthors.ExecuteReader()
While dtrAuthors.Read()
Listbox1.Items.Add(dtrAuthors("email"))
End While
dtrAuthors.Close()
conPubs.Close()

Would I have to add an index value to the listbox along with the "email" value? Right now, it populates the listbox1 but then can't select anything to add into listbox2.|||What I think is if you want to add some selected items from a list box to another list box, you could do it directly.

Read through (Traverse) the ListBox1. Write an "if" statement which selects only the selected values and add them directly to the ListBox2.

This is what I'm saying.

Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
ListBox2.Items.Add(ListBox1.Items(i).Text)
End If
Next
End Sub

If you want to use a key, value pair then you might have to use a Hash Table.

Sunday, February 19, 2012

DataBase Query help

Here's my situation:

I've got a column in one table called PRODID, and each value in this column references 1 or more values, say OrderDates, and I was wondering whether it's possible to write a query that returns all the values in the PRODID along with the latest OrderDate of the relevant PRODID value into the following result set:

PRODID OrderDate
1 "Latest Date"
2 "Latest Date"

And so on. Latest Date refers to the DateTime shown under OrderDates.

All help appreciatedSELECT P.ProductID, MAX(O.OrderDate)
FROM Orders O
INNER JOIN Products P ON O.ProductID = P.ProductID
GROUP BY P.ProductID|||Thanks for that, but I was just wondering is there any way to modify that query so that if any values for ProdID do not have an orderDate, the result set also returns these ProdIds but with a null value for the OrderDate?|||Actually ignore my last post, I managed to modify it to suit my eact needs, thanks for showing me the code though

Database Query

Is it possible to return the column names from the database using a SQL query,
what i need to do is
SELECT * FROM FEATURES WHERE 'VALUE' = 'YES'

i have a table which has a list of features and if they are selected i store the value yes, otherwise no . i want to be able to display a list of the features from the tables which have the value yes ! is this possible?yes xcept you dont need quotes around the column name..


SELECT * FROM FEATURES WHERE VALUE = 'YES'

hth|||Cheers but I worded the problem badly, VALUE isnt the name of the column, their are a number of different columns, each with different names, and i only want to display the column name if the value is yes ! any idea's?|||you wound then need to use CASE statement...check BOL for xact syntax but its something like


...
CASE
WHEN
colname='yes' then colname
ELSE NULL

hth|||what is BOL?|||BOL = Books Online

It is SQL Server's documentation.