2012-09-14 65 views
0

正在從數據庫獲取數據並存儲在dataset.dataset中包含電話號碼列表。 我想找出每個電話號碼的長度。如果長度爲十,則表示將其添加到一個數據表中。或者長度大於10意味着從電話號碼的右側獲得10個字符並將其存儲在相同的數據表中。這裏是我的代碼。當我調試代碼時,我只得到8000個行。但最初數據集包含40,700行值。在
數據表後達到8000行我得到錯誤在vb.net中長度不能小於零參數名稱長度錯誤

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

你不能在Substring方法使用負的長度。從長度上減去10來獲取你想要的字符串的起點:當你想從該點的字符串的其餘部分

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

,實際上是不需要第二個參數:

rsltstring = chkdphone.Substring(chkdphone.Length - 10) 
+0

我會盡力。非常感謝你的回覆 – vps

+0

如果我有一個電話號碼123456789012 na結果會是這樣的3456789012. ur commend ii給出這樣的答案 – vps

+0

主要條件是電話no長度> = 10意味着我想從右邊檢索char方(10個字符,我想採取) – vps

相關問題