0

I have this code:

 Dim dv As New DataView(_DataSet.Tables(0))
        ' select deleted rows 
        dv.RowStateFilter = DataViewRowState.Deleted
        For _irow As Long = 0 To dv.Table.Rows.Count - 1
            If Not IsDBNull(dv!serial) Then
                txtSerial.Text = (dv!serial).ToString
                Dim strconnection As String = "Data Source=EASMAR-PC;Initial Catalog=Database Connection;Integrated Security=True;"
                Dim _cn As SqlConnection = New SqlConnection(strconnection)
                _cn.Open()
                Dim cmd As New SqlCommand
                cmd.CommandText = "Delete from tblCustomer where serial= " & (dv!serial).ToString
            End If
        Next

I am just looping trough the dataview(If the rowwstate is deleted, the delete from database).

I am getting this error: Conversion from string "serial" to type 'Integer' is not valid. on this line : If Not IsDBNull(dv!serial) Then

Any ideas?

  • possible duplicate of [Conversion from string "" to long is not valid](http://stackoverflow.com/questions/14726120/conversion-from-string-to-long-is-not-valid) – varocarbas Jan 06 '14 at 09:07
  • PS: the actual duplicate is http://stackoverflow.com/questions/20934242/conversion-from-string-to-type-long-is-not-valid (but as far as it does not have any upvoted/accepted answers, it cannot be set as the "official duplicate"). – varocarbas Jan 06 '14 at 09:10
  • Ok. One more thing; What should the type, of "serial", be in the database? – user2968155 Jan 06 '14 at 10:09
  • That's what confusing me. I have the serial as an integer in the database. And i get this error: Conversion from string "serial" to type 'Integer' is not valid. – user2968155 Jan 06 '14 at 10:45
  • 1
    (I will try it again; but last time, at least, this soft). Imagine that you have two strings Dim string1 As String = "123" and Dim string2 As String = "oneTwoThree". Now imagine that you have an integer Dim int1 As Integer. If you do int1 = Convert.ToInt32(string1), it would work because string1 can be converted into an integer (even with Option Strict Off it might have been converted implicitely, without Convert.ToInt32). But if you try int1 = Convert.ToInt32(string2) it would ALWAYS output an error because "oneTwoThree" is not a number. Equivalently to "Jonathan" from the link above. – varocarbas Jan 06 '14 at 10:49
  • ... variables of type integer only can deal with values of type integer (= numbers). There are other types which might be converted to integer (as string1 in my example above) implicitely or explicitely (but a conversion always exists). But quite a few other variables which CANNOT BE CONVERTED TO THE GIVEN TYPE and thus will always output an error. This is kindergarten of programming and SO is for somehow knowledgeable people (e.g., equivalent to high school and above). And you keep asking and keep asking the same thing; no matter how many -1s you get and how much people laugh at your question – varocarbas Jan 06 '14 at 10:52
  • CLARIFICATION: for the last statement. Nobody here laughs at your knowleddge (at least, not myself); but at you being completely outside your place. A kindergarten kid is completely and absolutely respect-worthy in kindergarten; if he goes to the university to apply his kindergarten knowledge and is not aware of what he is doing, he might be a bit fun-made worthy (mainly to tell him: "learn a bit more before coming back, please"). – varocarbas Jan 06 '14 at 10:54
  • Some non-helpful comments here. Legitimate question - albeit a simple error – Grantly Jun 24 '15 at 14:29

1 Answers1

0

You need to access the ROW (integer) and COLUMN of the DataView. You are getting an error because you are using a String (Serial) for the first argument which should be a Row index integer. Like 0 for the first row.

dtvRow(0)("Serial")  

in your case

dv(_irow)(serial)

could be helpful :)

Grantly
  • 2,546
  • 2
  • 21
  • 31