sting to int error

Nocty

New member
Joined
Dec 9, 2008
Messages
4
Programming Experience
Beginner
My code is getting the following error

System.InvalidCastException was unhandled
Message="Conversion from string "" to type 'Integer' is not valid."

My code is the following

Private Sub btnMyQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMyQuery.Click
Dim daPrice As RMDHDataSet.Ticket_PriceDataTable
Dim quant As Integer
Dim price As Double
Dim i As Integer
Dim price_id As Integer


quant = txtQuantity.Text
daPrice = Ticket_PriceTableAdapter.GetDataBy(quant)
price = Double.Parse(daPrice.Rows(0).Item("Ticket_Price"))
price_id = Integer.Parse(daPrice.Rows(0).Item("Ticket_Price_ID"))

txtcost_per_ticket.Text = price
txtTotal_cost.Text = price * quant

For i = 1 To quant
TicketsTableAdapter.InsertQuery(CInt(txtSalesGiftType.Text), CStr(price_id), txtSalesTicketStatus.Text, txtSalesDonorID.Text)
Next
End Sub

Private Sub chkgift_type_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkgift_type.CheckedChanged
If chkgift_type.Checked = True Then
txtSalesGiftType.Text = "yes"
Else : txtSalesGiftType.Text = "no"
End If
End Sub

I'm not understanding what is going on? Ugh any help would be great thank you in advance.
 
You're doing this:
VB.NET:
CInt(txtSalesGiftType.Text)
to convert the text contained in the TextBox to an Integer. If the TextBox is empty then how can the contents (an empty string) be converted? That's what the error message is saying: it's not valid to convert an empty string to an Integer. It gets done in learning projects for simplicity but, in the real world, you cannot just assume that the user will enter valid data just because you want them to. If it's at all possible for the user to enter invalid data then it's up to you to validate it before using it.
 
Back
Top