注册 登录
编程论坛 VB.NET论坛

高手救命啊!!!

hgl520 发布于 2008-03-26 13:18, 841 次点击
这个函数是获取数据填充到listbox的代码,当从数据库删除一个数据后,Dim dr As DataRow = dt.Rows(0)就出错了,显示0处没有任何行   
 Private Sub getUser(ByVal id As String)
        Dim sql As String = "select * from [user] where UserId='" & id.Trim() & "'"
        Dim dt As DataTable = sample.GetDataTable(sql)
        Dim dr As DataRow = dt.Rows(0)
        If dt.Rows.Count <> 0 Then
            Me.tbId.Text = dr("UserId").ToString().Trim()
            Me.tbName.Text = dr("Username").ToString().Trim()
            Me.setcheck(dr("purview").ToString().Trim())
            Try
            Catch ex As Exception
                sample.WriteErr(ex)
            End Try
        End If
    End Sub
5 回复
#2
wugong2008-03-26 14:43
Private Sub getUser(ByVal id As String)
        Dim sql As String = "select * from [user] where UserId='" & id.Trim() & "'"
        Dim dt As DataTable = sample.GetDataTable(sql)
if dt.rows.count>0 then
Dim dr As DataRow = dt.Rows(0)
        If dt.Rows.Count <> 0 Then
            Me.tbId.Text = dr("UserId").ToString().Trim()
            Me.tbName.Text = dr("Username").ToString().Trim()
            Me.setcheck(dr("purview").ToString().Trim())
            Try
            Catch ex As Exception
                sample.WriteErr(ex)
            End Try
        End If
end if
end sub
#3
hgl5202008-03-27 11:29
回复 2# 的帖子
谢谢啊,问题是解决了,但是我删除的listbox里的索引出问题了,假设我的列表里有1,2,3,4,5这几个数,当我删除了3这个数,4的索引就会指向3了 ,很郁闷,3里面空的,甚么也没有了!
#4
hgl5202008-03-27 11:37
回复 2# 的帖子
也就是说,我怎么删除listbox里的索引值,我的删除代码是这样的
Private Function DelUser(ByVal id As String) As Boolean
        Dim flag As Boolean = True
        Try
            Dim sql As String = "delete from [user] where UserId='" & id.Trim() & "'"
            Dim sqlcmd As New SqlCommand(sql, sample.sqlcon)
            sample.sqlcon.Open()
            sqlcmd.ExecuteNonQuery()
        Catch ex As Exception
            sample.WriteErr(ex)
            flag = False
        Finally
            sample.sqlcon.Close()
        End Try
        Return flag
    End Function
#5
hgl5202008-03-27 11:37
高手帮忙哦
谢谢了46167057 我的vqq
#6
fairy42008-03-27 12:11
比較簡單點的方法,刪除之後,數據重載(相當于刷新)。這樣就不會發生你說的情況
如果一定要刪除的話!
listbox有個ITEMS屬性。那裡面有個方法可以讓你刪除那一行的!
1