ASP.net 2.0 Problem

Nayru
07-18-2006, 10:01 AM
I am probably missing something incredibly obvious, but I've compared this code to something else I wrote that does the exact same thing and don't see any reason why one works and one doesn't. (Maybe I just need more coffee)

I've got a form with 2 list boxes, one box has all of the modules required by a user, the other box has every other module. Pretty simple. There's 2 buttons to move selected items between the boxes which is where I run in to issues. Nothing throws an error but nothing happens when the buttons are pressed, the page posts back but the modules are never moved between the boxes.

One button calls AddRem with "Add" the other with "Rem"


Private Sub AddRem(ByVal action As String)

Dim cmdUpdate As OleDbCommand
Dim strSQL As String

If action = "Add" Then
Try
If Conn.State = ConnectionState.Closed Then Conn.Open()
For Each li As ListItem In lstNotReqd.Items
If li.Selected Then
strSQL = "Insert INTO trainingrecords (employee, moduleid, firstroundrequired) Values (" & Request.QueryString("empID") & ", " & li.Value & ", true)"
cmdUpdate = New OleDbCommand(strSQL, Conn)
cmdUpdate.ExecuteNonQuery()
End If
Next
Catch ex As Exception
txtEmpName.Text = ex.ToString
Finally
Conn.Close()
End Try
Else
Try
If Conn.State = ConnectionState.Closed Then Conn.Open()
For Each li As ListItem In lstReqd.Items
If li.Selected Then
strSQL = "Delete from trainingrecords where employee = " & Request.QueryString("EmpID") & " and moduleid = " & li.Value _
& " and createdbydept <> 2"
cmdUpdate = New OleDbCommand(strSQL, Conn)
cmdUpdate.ExecuteNonQuery()
End If
Next
Catch ex As Exception
txtEmpName.Text = ex.ToString
Finally
Conn.Close()
End Try
End If
GetRecs()
End Sub


Like I said the try catch blocks there never actually throw an error which is what's confusing me, and the values are never inserted into the database. The reason records cannot be deleted if createdbydept = 2 is because those are mandatory modules created by HR, HR has a seperate admin program they can use to add/remove those records, this one is for general purpose users.

The list boxes are populated through this sub:

Private Sub GetRecs()
Dim rdr As OleDbDataReader
Dim li As New ListItem
Dim cmd As New OleDbCommand("select Modules.ModuleName, TrainingRecords.FirstRoundRequired, " _
& " Modules.Moduleid FROM TrainingRecords inner JOIN Modules ON TrainingRecords.ModuleID = Modules.ModuleID WHERE " _
& " TrainingRecords.Employee = " & Request.QueryString("EmpID") & " order by modules.modulename", Conn)

Try
If Conn.State = ConnectionState.Closed Then Conn.Open()

rdr = cmd.ExecuteReader

lstReqd.DataSource = rdr
lstReqd.DataTextField = "modulename"
lstReqd.DataValueField = "moduleid"

lstReqd.DataBind()

rdr.Close()

cmd.CommandText = "Select modulename, moduleid from modules where moduleid not in (select moduleid " _
& " FROM TrainingRecords WHERE Employee = " _
& Request.QueryString("EmpID") & ") order by modulename"

rdr = cmd.ExecuteReader

lstNotReqd.DataSource = rdr
lstNotReqd.DataTextField = "modulename"
lstNotReqd.DataValueField = "moduleid"

lstNotReqd.DataBind()

Catch ex As Exception
txtEmpName.Text = ex.ToString
End Try
End Sub


Like I said this is probably something glaringly obvious that I've just overlooked and can't find, but any input would be appreciated. :huh:

Edit:
It actually looks like the li items in AddRem aren't retaining their selected state when they're created.. hmm

obajaber
07-18-2006, 08:15 PM
what's in your page load?

wayneph
07-19-2006, 07:10 AM
It sounds like you are rebinding your grid before you run the code to do the updates. That would cause it to "forget" what you asked for. Make sure you only rebind your grids after you handle their post back events.

Nayru
07-19-2006, 01:31 PM
Thanks for the input, I ended up having to scrap the listboxes because it was too complicated for HR.

Now it's just a big ugly list of 50+ check boxes :(

But your suggestion of it rebinding was probably correct, I think in testing I took out the test for post back so it probably was reloading the grid, I just completely missed it.

Thanks again

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum