Conversion from string "" to type 'Integer' is not valid.

When I run this code (button click) I recieve this error...
Conversion from string "" to type 'Integer' is not valid.
I have tried everything I can find online and in forums but without success.
The error points to line with----bcode = CInt(barcode(i))----

'declare array
Dim Irequests(8) As String
Dim barcode(8) As String
'assign values to array indexes
Irequests(0) = txtreq0.Text
Irequests(1) = txtreq1.Text
Irequests(2) = txtreq2.Text
Irequests(3) = txtreq3.Text
Irequests(4) = txtreq4.Text
Irequests(5) = txtreq5.Text
Irequests(6) = txtreq6.Text
Irequests(7) = txtreq7.Text

barcode(0) = txtBarcode0.Text
barcode(1) = txtBarcode1.Text
barcode(2) = txtBarcode2.Text
barcode(3) = txtBarcode3.Text
barcode(4) = txtBarcode4.Text
barcode(5) = txtBarcode5.Text
barcode(6) = txtBarcode6.Text
barcode(7) = txtBarcode7.Text

'declare vars

'Dim b As Integer = 0
Dim nhi As String = txtNHI.Text
Dim finishdate As String = txtFinishDate.Text
Dim accnumber As String = txtAccnumber.Text
Dim eventnumber As String = txtEventnumber.Text
Dim startdate As String = txtIssueDate.Text
Dim requestor As String = lblRequestor.Text
Dim requestdate As String = lblRequestDate.Text
Dim requireddate As String = lblRequiredDate.Text
Dim issuer As String = txtusername.Text
Dim reqid As String = DropDownList2.SelectedValue
Dim i As Integer = 0
Dim bcode As Integer
'Connection String value
Dim conn As String = ConfigurationManager.ConnectionStrings("LoansConnectionString").ConnectionString

'Create a SqlConnection instance
Using myConnection As New SqlConnection(conn)
'open connection
myConnection.Open()
For i = 0 To 7

bcode = CInt(barcode(i)) '<---------error here----

lblbcode2.Text = "this is bcode value: --'" & bcode & "'--"
'Specify the SQL query Insert Into Loan
Dim sql As String = "set DATEFORMAT dmy; insert into loan ( [NHI],[Start_Date], [Finish_Date],[ACC_Number], [Event_Number], [Req_ID] ) " & _
"values ( @nhi,@startdate,@finishdate,@accnumber,@eventnumber,@reqid)"
Dim myCommand As New SqlCommand(sql, myConnection)
Dim pnhi As New SqlParameter("@nhi", nhi)
Dim pstartdate As New SqlParameter("@startdate", startdate)
Dim pfinishdate As New SqlParameter("@finishdate ", finishdate)
Dim peventnumber As New SqlParameter("@eventnumber", CStr(eventnumber))
Dim paccnumber As New SqlParameter("@accnumber", CStr(accnumber))
Dim preqid As New SqlParameter("@reqid", reqid)

myCommand.Parameters.Add(pnhi)
myCommand.Parameters.Add(pstartdate)
myCommand.Parameters.Add(pfinishdate)
myCommand.Parameters.Add(peventnumber)
myCommand.Parameters.Add(paccnumber)
myCommand.Parameters.Add(preqid)
myCommand.ExecuteNonQuery()
Dim username As String = CStr(Session("username"))

Dim sql1 As String = "insert into Loan_Equipment ([Event_Number], [Issuer_Username],[Requestor],[Barcode], [Loan_ID]) " & _
" select '" & eventnumber & "','" & username & "','" & requestor & "', " & bcode & " , max([Loan_ID]) from loan "
Dim myCommand1 As New SqlCommand(sql1, myConnection)
myCommand1.ExecuteNonQuery()

Dim sql2 As String = " update Equipment set [Available_for_Loan] = 0 where barcode = '" & bcode & "'"
Dim myCommand2 As New SqlCommand(sql2, myConnection)
myCommand2.ExecuteNonQuery()

Dim sql4 As String = " update requests set [issued] = 1 where req_id = '" & reqid & "'"
Dim myCommand4 As New SqlCommand(sql4, myConnection)
myCommand4.ExecuteNonQuery()
Next
'Close the connection
myConnection.Close()
End Using


Answer this question

Conversion from string "" to type 'Integer' is not valid.

  • Cla82

    Ur code is trying to convert an empty string to Integer. Which is not valid.

    before this line bcode = CInt(barcode(i)) add an If statement. If barcode(i) = "" then bcode = 0 else ...

    u can also string.IsNullOrEmpty function



  • Conversion from string "" to type 'Integer' is not valid.