错误“位置0处没有行” [英] Error "There is no row at position 0"
问题描述
Hello Every One,
当我运行我的项目时,我得到错误0位没有行
,请帮忙。
这是我的代码:
Hello Every One,
When i run my project i get Error "There is no row at position 0"
,please help.
Here is my code:
Private Sub PopulateDepartmentHead()
'Fill data in comboBox
Dim SqlDataAdapter As OleDbDataAdapter
Dim DSet As New DataSet
Dim strSelect As String
SQLConn.Close()
SQLConn.ConnectionString = oFunc.GetConnectionString(sINIFile)
'myConn = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & strPath & "\" & strName & ".mdf" & ";Integrated Security=True;User Instance=True")
SQLConn.Open()
Try
strSelect = "SELECT * FROM dbo.hrEmployeeInformation"
SqlDataAdapter = New OleDbDataAdapter(strSelect, SQLConn)
DSet = New DataSet()
SqlDataAdapter.Fill(DSet)
Me.dhcboEmployeeSelect.DataSource = DSet.Tables(0)
Me.dhcboEmployeeSelect.ValueMember = "eiEmployeeID"
Me.dhcboEmployeeSelect.DisplayMember = "eiFirstName"
'MsgBox(Me.dhcboEmployeeSelect.DisplayMember = "Cust_ID")
SQLConn.Close()
Catch ex As Exception
MessageBox.Show("Error No : " & Err.Number & vbCrLf _
& "Error : " & ex.Message & vbCrLf _
& "Source : " & Err.Source & vbCrLf _
, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
selectindexchanged事件的代码:
Code for selectindexchanged event:
Private Sub dhcboEmployeeSelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dhcboEmployeeSelect.SelectedIndexChanged
Dim SqlDataAdapter As OleDbDataAdapter
Dim custDataset As New DataSet
Dim strSelect As String
SQLConn.Close()
SQLConn.ConnectionString = oFunc.GetConnectionString(sINIFile)
SQLConn.Open()
Try
strSelect = "SELECT * FROM dbo.hrEmployeeInformation WHERE eiEmployeeID = '" & dhcboEmployeeSelect.Text & "'"
SqlDataAdapter = New OleDbDataAdapter(strSelect, SQLConn)
custDataset = New DataSet()
SqlDataAdapter.Fill(custDataset, "dbo.hrEmployeeInformation")
SqlDataAdapter.Dispose()
Dim custTable As DataTable = custDataset.Tables(0)
dhEmployeeID.Text = custTable.Rows(0).Item(0)
dhEmployeeName.Text = custTable.Rows(0).Item(1)
SQLConn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
SQLConn.Close()
End Try
End Sub
表格载入代码:
Code for Form Load:
Private Sub frmDepartmentHead_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
SQLConn.Close()
SQLConn.ConnectionString = oFunc.GetConnectionString(sINIFile)
FormName()
EnableControlsFormLoad(True)
SQLConn.Open()
ds.Clear()
SQLConn.Close()
'Fill Combo
PopulateDepartmentHead()
Catch ex As Exception
MessageBox.Show("Error No : " & Err.Number & vbCrLf _
& "Error : " & ex.Message & vbCrLf _
& "Source : " & Err.Source & vbCrLf _
, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
请帮助我,我哪里错了......
谢谢
PLEASE HELP ME, WHERE DID I WENT WRONG....
Thanks
推荐答案
Me.dhcboEmployeeSelect.DataSource = DSet.Tables(0)
我认为这行你得到了错误信息。
问题是你的DataSet没有0表。
但你尝试调用表[0]。发生此错误的方式。
一旦尝试执行您的SQL查询,您的查询将返回任何值,或者不返回结账...
I think this line you got that error message .
What''s the problem in that is your DataSet doesn''t have "0"th Tables.
But your try to call the table[0]. that''s way this error occured.
Once try to execute your SQL query ,your query returns any value or not checkout that..
strSelect = "SELECT * FROM dbo.hrEmployeeInformation WHERE eiEmployeeID = '" & dhcboEmployeeSelect.Text & "'"
检查此行是否返回任何结果或为空..
check this line returns any result or empty..
在行
dhEmployeeID.Text = custTable.Rows(0).Item(0)
在访问之前,您应该始终检查表中是否至少有一行。
您可以使用
You should always check if there is at least one row in table before accessing it.
You can do this with
custTable.Rows.Count > 0
同样可以说行
The same can be said for line
Dim custTable As DataTable = custDataset.Tables(0)
使用DataAdapter时可以消除几点积分
1.您无需打开或关闭连接,因为DataAdapter会为您执行此操作。
2.您不应该在DataAdapter上调用dispose。理想情况下,您应该保留相同的DataAdapter,因为它已经执行了它的初始化。 DataAdapter提供了诸如SelectCommand,UpdateCommand,InsertCommand和DeleteCommand之类的属性,允许您设置不同的Command对象以在数据源上执行这些不同的功能。所以,你看,DataAdapter被设计为可以重用于多个命令(用于相同的数据库连接)。
Also there are few points which can be eliminated when using DataAdapter
1. You do not need to open or close connection as DataAdapter does this for you.
2. You should not call dispose on DataAdapter. Ideally, you should retain the same DataAdapter because it has already performed it''s initialization. A DataAdapter provides properties such as the SelectCommand, UpdateCommand, InsertCommand and DeleteCommand which allow you to set different Command objects to perform these different function on the datasource. So, you see, the DataAdapter is designed to be reused for multiple commands (for the same database connection).
嗨亲爱的,
请检查
hi dear,
please check
If DSet.Tables[0] is not nothing and DSet.Tables(0).Rows.Count > 0
将数据源分配给dhcboEmployeeSelect
before assign datasource to dhcboEmployeeSelect
这篇关于错误“位置0处没有行”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!