Wednesday, March 21, 2012

ListBox Mutli-Select through a For Each Loop

Hi everyone, not sure if a this topic has been covered yet (a have been looking all day), but as I am still very new to this, my problem is as follows:

In the Try .. Catch block below, data is posted from a form and the SqlCommand.ExecuteScalar() statement returns a Unique Job ID.

I am attempting to populate a subordinate table for qualifications which are selected from a ListBox, but rather than using qualification titles, I am using the values.

My problem is that only one value (the first) gets posted multiple times, when multiple values are selected.

Looking at the For Each loop in the inner Try Catch block, I am wondering whether there is some sort of Index pointer that needs to be incremented, in order to establish new values further down the list.

I have seen no evidence that this is the case, save for the fact that the value stalls on just the first.

Any help would be appreciated.

===== CODE ===

Try

C4LConnection.Open()

JobPostingID = SqlJobPost.ExecuteScalar()

Response.Write("<br />Selected Item: " & Qualifications.SelectedItem.Value)

Try

' Multiple Qualification Entries

SqlQualPost.Parameters.Add(New SqlParameter("@.JobPostingID", SqlDbType.Int))

SqlQualPost.Parameters("@.JobPostingID").Value = Int32.Parse(JobPostingID)

SqlQualPost.Parameters.Add(New SqlParameter("@.QualificationID", SqlDbType.Int))

SqlQualPost.Parameters("@.QualificationID").Value = Int32.Parse(Qualifications.SelectedItem.Value)

If Qualifications.SelectedIndex > -1Then

ForEach ItemIn Qualifications.Items

If Item.SelectedThen

Response.Write("<br />SelectedItem Value: " & Qualifications.SelectedItem.Text)

QualPostingID = SqlQualPost.ExecuteScalar()

SqlQualPost.Parameters("@.QualificationID").Value = Int32.Parse(Qualifications.SelectedItem.Value)

Response.Write("<br />Selected Item: " & Qualifications.SelectedItem.Value)

EndIf

Next

EndIf

Catch ExpAs SqlException

failJobPost =True

lblError.Visible =True

lblError.Text ="Could not add qualifications <br />" & Exp.Message

EndTry

failJobPost =False

Catch ExpAs SqlException

failJobPost =True

lblError.Visible =True

lblError.Text ="Error: could not post job to database <br />" & Exp.Message

Finally

C4LConnection.Close()

EndTry

Hello Orion,

Orion Blue:

SqlQualPost.Parameters("@.QualificationID").Value = Int32.Parse(Qualifications.SelectedItem.Value)

In this statement you should use Item.Value. Currently you are looping through the items, but always refer to Qualifications.SelectedItem to get an ID.

|||

Thanks for the answer. Interestingly, I discovered the answer myself through trial and error. Once again, thanks for the reply.

No comments:

Post a Comment