2013-05-16 21 views
-1

我有一個gridview,我在check_changed上插入複選框,即使我編寫以下代碼以獲取問題Id的值,如圖中所示,也顯示了文本框中的值,但是當我使用這些串中的SQL:在asp.net中將字符串轉換爲整數

Select * from IssueBook Where IssueId IN (values) 

它顯示誤差變換VARCHAR到數字上check_changed我寫這些代碼我有采取IssueId(數字)

Protected Sub CheckBox1_CheckedChanged1(ByVal sender As Object, ByVal e As System.EventArgs) 
     Dim x As String = "" 
     For Each row As GridViewRow In GridView1.Rows 
      Dim cb As CheckBox = row.FindControl("checkbox1") 
      If cb IsNot Nothing AndAlso cb.Checked Then 
       If x <> "" Then 
        x += "," 
       End If 
       x += row.Cells(1).Text 
      End If 
     Next 
     RwId.Text = x 
     Session("SelctdIsuedBokNo") = RwId.Text 

在我以前的數值數據表。 如何使用逗號分隔符將以上代碼轉換爲整數,例如4,5,6,

+0

您的代碼對於SQL查詢來說究竟是什麼樣子,以及發生錯誤的確切位置?我不認爲這是所有的代碼。 – LittleBobbyTables

回答

1

IN語句需要一個逗號分隔值列表,而不是帶有這些值的「字符串」。

您應該創建一個動態SQL並將它傳遞給你的命令(或SqlCommand的任何方法使用的是執行SQL)...是這樣的:(如果你正在使用的SqlClient)

SqlCommand cmd = new SqlCommand(String.Format("Select * from IssueBook Where IssueId IN ({0})", values), your_connection); 

希望它幫助。

+0

許多感謝我的問題已解決,工作正常。 –

+0

它現在工作謝謝回覆 –

0

嘗試使用

Integer.Parse() 

Integer.TryParse() 

希望工程。

0

不能將字符串「4,5,6」轉換爲單個整數值。

最好的辦法是改變select來接受varchar值。

0

在這種情況下,爲了避免sql注入,並且只要您的IDs(Cells(1).Text值)是整數,您應該從單元中收集您的ID並在整數列表中鍵入它們的安全,然後使用逗號到末尾的字符串

我已經相應地調整了您的代碼。

Protected Sub CheckBox1_CheckedChanged1(ByVal sender As Object, ByVal e As System.EventArgs) 
    Dim Values As List(Of Integer)'your integer list of IDs 
    For Each row As GridViewRow In GridView1.Rows 
     Dim cb As CheckBox = row.FindControl("checkbox1") 
     If cb IsNot Nothing AndAlso cb.Checked Then 
      Values.Add(Integer.convert(row.Cells(1).Text))'add the ID 
     End If 
    Next 
    RwId.Text = String.Join(",", Values)'pull out the list into a comma delimited string eg "2,45,67,87" 
    Session("SelctdIsuedBokNo") = RwId.Text 
+0

在上面提到的腳本中有兩個以下的錯誤善意詳細錯誤'轉換'是一種類型,不能用作表達式。 \t 錯誤'System.Collections.Generic.List(Of Integer)'類型的值無法轉換爲'String的一維數組'。 –