here's my effort:
Sub AddStudentToModule_Click(sender As Object, e As EventArgs)
connection = New SqlConnection(ConfigurationSettings.AppSettings("Connection"))
cmdInsert = New SqlCommand("spAddStudentToModule", connection)
cmdInsert.CommandType = CommandType.StoredProcedure
cmdInsert.Parameters.Add("@dotnet.itags.org.StudentID", lbAddStudentsToModule.SelectedValue )
cmdInsert.Parameters.Add("@dotnet.itags.org.ModuleID", ddModuleAddLookUp.SelectedValue)
Dim infoList As String
Dim i As Integer
For i = 0 To lbAddStudentsToModule.Items.Count - 1
IflbAddStudentsToModule.Items(i).Selected = True Then
infoList &=lbAddStudentsToModule.Items(i).Value
End If
Next
connection.Open()
cmdInsert.ExecuteNonQuery()
connection.Close()
End Sub
any help would be appreciated.
First, not sure if this matters...
infoList+= lbAddStudentsToModule.Items(i).Value
But, here's another way to achieve your goal...
connection.Open()
Dim i As Integer
For i = 0 To lbAddStudentsToModule.Items.Count - 1
If lbAddStudentsToModule.Items(i).Selected = True Then
cmdInsert.Parameters.Clear()
cmdInsert.Parameters.Add("@.StudentID", lbAddStudentsToModule.Items(i).Value )
cmdInsert.Parameters.Add("@.ModuleID", ddModuleAddLookUp.Items(i).Value)
cmdInsert.ExecuteNonQuery()
End If
Next
connection.Close()
Hope you are trying to put in names, a new one on a new row in the db.
Zath
hello,
thanks for your help.
I'm not trying to put names into the db, just numbers: an id for the student and an id for the module.
however, I got the following message:
Error converting data type nvarchar to int.
How do i reference the value for the listbox item and thedropdownlist item. I think that it is trying to send the textvaules back to the db at the moment.
CInt(myVar)
That should convert it to an int, myVar being the variable you are wanting to convert.
But could be in the database. Check to see that the field is set to int and not nvchar.
Zath
Thanks for your help, but i'm still stumped as I don't understand where I should be putting the CInt(myVar) in this:
cmdInsert.Parameters.Add("@.StudentID", lbAddStudentsToModule.Items(i).Value )
I checked in the Db and the values are integers in the db with thedatatype int, so I don't understand why it is getting changed from thatvalue to another.
i also don't think i understand some other aspects of your code, for example
cmdInsert.Parameters.Add("@.StudentID", lbAddStudentsToModule.Items(i).Value )
why isn't it referencingddModuleAddLookUp.SelectedValue,if it is looking at all the items, then it may be passing the text andnot the value. the text is only there to help the user, i'm notinterested in the text, i just want the values from both lists to postback to the db.
to test this, I changed the text value of the listbox to be the same as the integer value:
lbAddStudentsToModule.DataTextField = "StudentID"
lbAddStudentsToModule.DataValueField = "StudentID"
and I still got the same error message. i'm very confused now.
i hope this is clear.
0 comments:
Post a Comment