0

I have this skill checkboxlist which contains skills that can be selected by the user. If the user select two skills,two records will be inserted to the table. I tried this one:

        Dim varSkillID As Integer()
        varSkillID = split(skills, ",")
        If varSkillID(0).value > 0 Then
            Dim sql As String = Nothing
            For I = 0 To varSkillID()
                sql = "INSERT INTO tblConstituentSkills (skillID) VALUES ( " & varSkillID.Value & ")"
            Next I
       end if

but its not working. Thanks!

I also tried this code.

        Dim varSkillID() As String = Split(skillID, ",")

        For i As Integer = 0 To varSkillID.Length - 1
            If varSkillID(i) <> "" Then
                Using sql As New SqlProcedure("spInsertSkill")
                    sql.AddParameter("@ConstituentIdNo", constituentIdNo)
                    sql.AddParameter("@skillID", varSkillID(i))
                    sql.ExecuteNonQuery()
                End Using
            End If

        Next i

It works when I only select single skill. But if I select two or more skills this error appears "Nullable object must have a value."

roger bulawan
  • 167
  • 1
  • 1
  • 11

2 Answers2

1
  • please use the editor help to design your request. It is very hard to read.
  • What does the errormessage say?
  • There is an End If missing
  • Where does constituentIdNo.Value is coming from?

To call the Sub:

Call r101("123456", "1,2,3,4")

the Sub:

Public Sub r101(ByVal constituentIdNo As String, ByVal skills As String)
    Dim varSkillID() As String = Split(skills, ",")
    Dim sql As String = Nothing

    For i As Integer = 0 To varSkillID.Length - 1
        If varSkillID(i) <> "" Then sql = "INSERT INTO tblConstituentSkills (ConstituentIdNo, skillID) VALUES (" & constituentIdNo & ", " & varSkillID(i) & ")"
    Next i

End Sub

This is not the cleanest code, but the best I could create from your given feedback. I don't see why to convert skills to integer.

Community
  • 1
  • 1
pLurchi
  • 61
  • 5
  • Thank you for giving attention to the question. I am new to vb.net. I have edited the code above for better reading. The error message is "Value of type '1-dimensional array of String' cannot be converted to '1-dimensional array of Integer' because 'String' is not derived from 'Integer'.". Thank you. – roger bulawan Jul 21 '16 at 06:31
  • Does the error occur in row 2? Are you sure that only numbers separated by comma stored inside skills variable? – pLurchi Jul 21 '16 at 06:42
  • Please add the line of code, where you initiate and set variable "skills" and constituentIdno. – pLurchi Jul 21 '16 at 06:50
  • Yes. The error occurs in row 2 and I am sure that only numbers separated by comma is stored inside skills variable. – roger bulawan Jul 21 '16 at 06:53
  • Ok, please check your code. You are using three variables, named likely the same. skillID, varSkillID and varSkill. I think your If-clause should be changed to "If varSkill(0).value > 0 Then". – pLurchi Jul 21 '16 at 07:03
  • I'm sorry it was typo error. It should be varSkillID not varSkill. Only two variables. I changed my code here but the same error message. – roger bulawan Jul 21 '16 at 07:07
  • Thank you. The above code you gave does not insert anything or maybe i just don't know what is lacking to insert the values. skills field is a look up field from tblSkills where data value is an integer which is the skillID from tblSkills.Thanks. – roger bulawan Jul 26 '16 at 00:38
  • Is there any error message in the code left? If there is nothing inserted in database, then it might be a problem with the statement itself. But actually, only the varaible "sql" is filled in this snippet. – pLurchi Jul 26 '16 at 06:53
  • It show no error but nothing inserted in the table. I tried editing the code by using stored procedure (code above) but still no lack. – roger bulawan Jul 27 '16 at 05:37
0

I just read this and saved me.separate comma separated values and store in table in sql server Thanks stackoverflow!you're a savior!

Community
  • 1
  • 1
roger bulawan
  • 167
  • 1
  • 1
  • 11