Fosa
Fosa

Reputation: 457

SQL Statement as datasource for listbox VB.NET

ok so I got a listbox which I made the displaymember equal to "ID"

I fill my dataset and if I make a change to my dataset the listbox will change also.

I'm filling my dataset with this kind of dataAdapter :

KamersDataAdapter = New SqlDataAdapter("SELECT ID FROM roomsTable", ConnectionString)

So far so good.

It would be cool if I could make an nice sentence So the user could read something nice in the listbox then just the database items. So I did this :

New SqlDataAdapter("SELECT *, convert(varchar,convert(date,StartDatum),103) +' - ' + convert(varchar,convert(date,EindDatum),103) + ': room ' + convert(varchar,KamerNummer) AS combinatie FROM VerhuringenTable", ConnectionString)

then instead of putting the displaymember to ID, I put it to Combinatie (dutch for combination)

Now if i fill my dataset for the first times everything goes fine. Then I get this:

18/01/2014-30/05/2014:room 103

But now when i Add an new room. the room doesnt want to show up in my listbox. with other words...my dataset is not updating or my listbox cant handle this query

I know it HAS to do with this sql statement cause my listbox updates fine when i Just select The ID.

Anyone knows whats goin on ?

thanx

fosa

Upvotes: 2

Views: 905

Answers (1)

Steve
Steve

Reputation: 216243

Your column combinatie is calculated at the extraction of your data from the database. After that the Dataset doesn't contains any rules that could be applied at runtime to generate the combinatie column from the data of the new Room. You should add the info for this field manually to the dataset table,
probably when you add the booking for the Room

 Dim newRow = ds.Tables("room").NewRow()
 newRow("StartDatum") = DateTime.Today
 newRow("EindDatum") = DateTime.Today.AddDays(7)
 newRow("KamerNummer") = roomNumber
 ....
 newRow("combinatie") = DateTime.Today.ToShortDateString() & "-" & _ 
                        DateTime.Today.AddDays(7).ToShortDateString() & _ 
                        ":room " & roomNumber.ToString()
ds.Tables("room").Rows.Add(newRow)

Upvotes: 1

Related Questions