我收到错误“0位置没有行”从这个vb.net代码 [英] I get an error "There is no row at position 0" from this vb.net code

查看:73
本文介绍了我收到错误“0位置没有行”从这个vb.net代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私有  Sub  Button3_Click( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs)句柄 Button3.Click 
< span class =code-keyword>如果 txtID.Text = 然后
MsgBox( 请输入有效的员工代码加载相应的记录,MsgBoxStyle.Information)
否则
dbProvider = Provider = Microsoft.Ace.OLEDB.12.0;
dbSource = Data Source = C:\Users\Blessing\Documents\IBCARIP.accdb; Persist Security Info = False
con.ConnectionString = dbProvider& ; dbSource
con.Open()
sql = select * from Calculator where _
& EmpCode =& '& txtID.Text& '

da = OleDb.OleDbDataAdapter(sql,con)
da.Fill(ds, IBCARIP
lblSAI.Text = ds.Tables( IBCARIP) .Rows(inc).Item( SName)& ds.Tables( IBCARIP)。Rows(inc).Item( FName
lblRate.Text = ds.Tables( IBCARIP)。Rows(inc).Item( NRate
lblOT.Text = ds.Tables( IBCARIP).Rows(inc).Item( OTRate
lblBnk。 Text = ds.Tables( IBCARIP)。Rows(inc).Item( BName)& ..& ds.Tables( IBCARIP)。Rows(inc).Item( ANumber)& ..& ds.Tables( IBCARIP)。Rows(inc).Item( AType
con.Close()
ds.Tables( IBCARIP)。DataSet.Clear()
MaxRows = ds.Tables( IBCARIP)。Rows.Count
' inc = 0
结束 如果
结束 Sub







当我向txtID.text输入无效ID时出现错误

解决方案

只需将您的代码放入以下内容阻止......



 如果 ds.Tabl es(  IBCARIP)。Rows.Count >   0 然后
' 写你的代码在这里......
结束如果


引用:

当我向txtID.text输入无效ID时出现错误

是的,它会抛出。因为无效的ID会从数据库中获取任何内容。



因此,您需要执行以下操作以检查查询是否返回任何行。

 如果 ds.Tables(  IBCARIP).Rows.Count>  0  然后 
lblSAI.Text = ds.Tables( IBCARIP)。Rows(inc).Item( SName)& ds.Tables( IBCARIP)。Rows(inc).Item( FName
lblRate.Text = ds.Tables( IBCARIP)。Rows(inc).Item( NRate
lblOT.Text = ds.Tables( IBCARIP).Rows(inc).Item( OTRate
lblBnk。 Text = ds.Tables( IBCARIP)。Rows(inc).Item( BName)& ..& ds.Tables( IBCARIP)。Rows(inc).Item( ANumber)& ..& ds.Tables( IBCARIP)。Rows(inc).Item( AType
con.Close()
ds.Tables( IBCARIP)。DataSet.Clear()
MaxRows = ds.Tables( IBCARIP)。Rows.Count
结束 如果


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If txtID.Text = "" Then
            MsgBox("Please input a valid Employee code to load a corresponding record", MsgBoxStyle.Information)
        Else
            dbProvider = "Provider=Microsoft.Ace.OLEDB.12.0;"
            dbSource = "Data Source = C:\Users\Blessing\Documents\IBCARIP.accdb;Persist Security Info=False"
            con.ConnectionString = dbProvider & dbSource
            con.Open()
            sql = "select * from Calculator where " _
                & "EmpCode = " & " '" & txtID.Text & "'"

            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "IBCARIP")
            lblSAI.Text = ds.Tables("IBCARIP").Rows(inc).Item("SName") & ds.Tables("IBCARIP").Rows(inc).Item("FName")
            lblRate.Text = ds.Tables("IBCARIP").Rows(inc).Item("NRate")
            lblOT.Text = ds.Tables("IBCARIP").Rows(inc).Item("OTRate")
            lblBnk.Text = ds.Tables("IBCARIP").Rows(inc).Item("BName") & ".." & ds.Tables("IBCARIP").Rows(inc).Item("ANumber") & ".." & ds.Tables("IBCARIP").Rows(inc).Item("AType")
            con.Close()
            ds.Tables("IBCARIP").DataSet.Clear()
            MaxRows = ds.Tables("IBCARIP").Rows.Count
            'inc = 0
        End If
    End Sub




the error come up when i input an invalid ID into txtID.text

解决方案

Just put your code to the following block...

if  ds.Tables("IBCARIP").Rows.Count > 0 Then
'write your code here.....
End if


Quote:

the error come up when i input an invalid ID into txtID.text

Yes, it will throw. Because invalid ID will get you nothing from the Database.

So, you need to do like below in order to check if the query returns any row or not.

If ds.Tables("IBCARIP").Rows.Count > 0 Then
    lblSAI.Text = ds.Tables("IBCARIP").Rows(inc).Item("SName") & ds.Tables("IBCARIP").Rows(inc).Item("FName")
    lblRate.Text = ds.Tables("IBCARIP").Rows(inc).Item("NRate")
    lblOT.Text = ds.Tables("IBCARIP").Rows(inc).Item("OTRate")
    lblBnk.Text = ds.Tables("IBCARIP").Rows(inc).Item("BName") & ".." & ds.Tables("IBCARIP").Rows(inc).Item("ANumber") & ".." & ds.Tables("IBCARIP").Rows(inc).Item("AType")
    con.Close()
    ds.Tables("IBCARIP").DataSet.Clear()
    MaxRows = ds.Tables("IBCARIP").Rows.Count
End If


这篇关于我收到错误“0位置没有行”从这个vb.net代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆