vb.net中的长度不能小于零参数名称长度错误

时间:2012-09-14 05:32:29

标签: string datatable dataset substring vb.net-2010

从数据库获取数据并在dataset.dataset中存储包含电话号码列表。 我想找出每个电话号码的长度。如果长度为10则意味着将其添加到一个数据表中。或者长度大于10意味着从电话号码的右侧获取10个字符并将其存储在相同的数据表中。这是我的代码。当我调试代码时,我只获得8000个行数。但最初数据集包含40,700个行值。之后 数据表达到错误的8000行

my code
-------
  ada.Fill(ds, "reports.renewal_contact_t ")

                ds.Tables(0).DefaultView.RowFilter = " PHONE NOT like'0'"
                dt = ds.Tables(0)
                For Each q In dt.Rows
                    chkphone = q("PHONE").ToString
                    chkdphone = Regex.Replace(chkphone, "[^\d]", "")

                    'MessageBox.Show(chkdphone)


                    If (chkdphone.Length = 10) Then
                        Dim anyRow As DataRow = dt2.NewRow
                        anyRow(0) = chkphone.ToString
                        dt2.Rows.Add(anyRow)

                    ElseIf (chkdphone.Length >= 10) Then
                        rsltstring = chkdphone.Substring(chkdphone.Length, -10)

                        Dim anyrow1 As DataRow = dt2.NewRow
                        anyrow1(0) = rsltstring.ToString
                        dt2.Rows.Add(anyrow1)
                    Else


                    End If
                Next

                new_ds.Tables.Add(dt2)
                ComboBox1.DataSource = new_ds.Tables(0)
                ComboBox1.DisplayMember = "PHONE" 



 Error
 -----
 length cant be less than zero parameter name length

1 个答案:

答案 0 :(得分:1)

您不能在Substring方法中使用负长度。从长度中减去10以获得所需字符串的起点:

rsltstring = chkdphone.Substring(chkdphone.Length - 10, 10)

如果您想要从该点开始的其余字符串,实际上不需要第二个参数:

rsltstring = chkdphone.Substring(chkdphone.Length - 10)
相关问题