错误,位置大于0没有行 [英] Error, no row at position greater than 0

查看:95
本文介绍了错误,位置大于0没有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用sql server数据库的vb .net winform项目。用户将访问工厂的人员(firstName,lastName,company,contactNumber等)的详细信息输入到文本框中,点击保存详细信息按钮,这将数据保存在数据表中。这很好,问题是下一部分。然后将用户重定向到另一个表单,其中输入详细信息显示在数据库中。这适用于第一个记录但不适用于之后的任何记录输入,我收到一条错误,上面写着在'n'处没有行',并且在form_Load中突出显示以下代码行:



 txtFirstName.Text =  CStr (dtVisitorDetails.Rows( CInt  CDbl (txtIdNumber.Text) -   1 ))。项目(< span class =code-digit> 1 ))



它告诉我第0行之后的任何行都没有,但我知道它们是因为我输入了它们,它们出现在sql server数据库管理器的数据表中。

我无法解决这个问题,对此有任何帮助将不胜感激。我附上了与此问题有关的其余代码。

先谢谢了。



 私有  Sub  previousVisitor_Load(发件人作为  Object ,e  As  EventArgs)句柄  MyBase  .Load 

connectionString = 数据Source = .\SQLExpress; InitialCatalog = Visitors;& _
IntegratedSecurity = True; MultipleActiveResultSets = True

sqlVisitorDetails = SELECT * FROM visitorDetails WHERE idNumber = @ idNumber

sqlCon .Open()
sqlCmd = SqlCommand(sqlVisitorDetails,sqlCon)
sqlCmd.Parameters.AddWithValue( @ idNumber,txtIdNumber.Text)

dtVisitorDetails = loadDtVisitorDetails()

txtFirstName .Text = CStr (dtVisitorDetails.Rows( CInt CDbl (txtIdNumber.Text) - 1 ))。项目( 1 ))
txtLastName.Text = CStr (dtVisitorDetails.Rows( CInt CDbl (txtIdNumber.Text) - 1 ))。项目( 2 ))
txtCompany.Text = CStr (dtVisitorDetails.Rows( CInt CDbl (txtIdNumber.Text) - 1 ))。项目( 3 ))
txtContactNumber.Text = CStr (dtVisitorDetails.Rows( CInt CDbl (txtIdNumber.Text) - 1 ))。项目( 4 ))
txtCountryCode.Text = CStr (dtVisitorDetails.Rows( CInt CDbl (txtIdNumber.Text) - 1 ))。项目( 5 ))
txtEmail.Text = CStr (dtV isitorDetails.Rows( CInt CDbl (txtIdNumber.Text) - 1 ))。项目( 7 ))

sqlCmd.Dispose()
sqlCon.Close()

结束 Sub

私有 函数 loadDtVisitorDetails() As DataTable
< span class =code-keyword> Dim dtVisitorDetails As DataTable = Nothing

sqlVisitorDetails = SELECT * FROM visitorDetails WHERE idNumber =& txtIdNumber.Text

dtVisitorDetails = fillDtVisitorDetails(sqlVisitorDetails)

返回 dtVisitorDetails
结束 功能

公共 函数 fillDtVisitorDetails( ByVal sqlVisitorDetails As 字符串作为 DataTable

Dim dtVisitorDetails As DataTable
Dim da 作为 SqlDataAdapter
Dim conCmd 作为 SqlCommand

conCmd.CommandText = sqlVisitorDetails

da.SelectCommand = conCmd
da.SelectCommand.Connection = sqlCon

dtVisitorDetails.Columns.GetEnumerator()
da。填充(dtVisitorDetails)

返回 dtVisitorDetails
结束 函数

解决方案

我改变了

 (txtIdNumber.Text) -   1 

到行中的'0'

 txtFirstName.Text =  CStr (dtVisitorDetails.Rows( CInt  CDbl ( txtIdNumber.Text) -   1 ))。项目( 1 ))

和lastName,company等的行。

因此该行现在读取

 txtFirstName.Text =  CStr ( dtVisitorDetails.Rows(  0 )。项目( 1 ))

已解决了我的问题。我认为,如果我有'0',那么每次都会返回'0'行(即ID号1),但事实并非如此,因为我在sql语句中查询ID号,所以它显示了ID号查询。


I am making a vb .net winform project that uses a sql server database. A user inputs the details of a person (firstName, lastName, company, contactNumber, etc.) visiting a factory into textboxes, hits the save details button, and this saves the data in the datatable. This works fine, the problem is the next part. The user is then redirected to another form where the input details are shown from the database. This works for the first record but not for any record input after that, I get an error that says "There is no row at position 'n'" and the following line of code is highlighted in the form_Load:

txtFirstName.Text = CStr(dtVisitorDetails.Rows(CInt(CDbl(txtIdNumber.Text) - 1)).Item(1))


It is telling me that any rows after row 0 are not there but I know they are because I have input them and they are showing up in the datatable in the sql server database manager.
I cannot sort this problem, any help with this would be greatly appreciated. I am attaching the rest of the code that's involved with this problem.
Thanks in advanced.

Private Sub previousVisitor_Load(sender As Object, e As EventArgs) Handles MyBase.Load

connectionString = "Data Source=.\SQLExpress;InitialCatalog=Visitors;" & _
"IntegratedSecurity=True;MultipleActiveResultSets=True"

sqlVisitorDetails = "SELECT * FROM visitorDetails WHERE idNumber=@idNumber"

    sqlCon.Open()
    sqlCmd = New SqlCommand(sqlVisitorDetails, sqlCon)
    sqlCmd.Parameters.AddWithValue("@idNumber", txtIdNumber.Text)

    dtVisitorDetails = loadDtVisitorDetails()

    txtFirstName.Text = CStr(dtVisitorDetails.Rows(CInt(CDbl(txtIdNumber.Text) - 1)).Item(1))
    txtLastName.Text = CStr(dtVisitorDetails.Rows(CInt(CDbl(txtIdNumber.Text) - 1)).Item(2))
    txtCompany.Text = CStr(dtVisitorDetails.Rows(CInt(CDbl(txtIdNumber.Text) - 1)).Item(3))
    txtContactNumber.Text = CStr(dtVisitorDetails.Rows(CInt(CDbl(txtIdNumber.Text) - 1)).Item(4))
    txtCountryCode.Text = CStr(dtVisitorDetails.Rows(CInt(CDbl(txtIdNumber.Text) - 1)).Item(5))
    txtEmail.Text = CStr(dtVisitorDetails.Rows(CInt(CDbl(txtIdNumber.Text) - 1)).Item(7))

    sqlCmd.Dispose()
    sqlCon.Close()

End Sub

Private Function loadDtVisitorDetails() As DataTable
    Dim dtVisitorDetails As DataTable = Nothing

    sqlVisitorDetails = "SELECT * FROM visitorDetails WHERE idNumber=" & txtIdNumber.Text

    dtVisitorDetails = fillDtVisitorDetails(sqlVisitorDetails)

    Return dtVisitorDetails
End Function

Public Function fillDtVisitorDetails(ByVal sqlVisitorDetails As String) As DataTable

    Dim dtVisitorDetails As New DataTable
    Dim da As New SqlDataAdapter
    Dim conCmd As New SqlCommand

    conCmd.CommandText = sqlVisitorDetails

    da.SelectCommand = conCmd
    da.SelectCommand.Connection = sqlCon

    dtVisitorDetails.Columns.GetEnumerator()
    da.Fill(dtVisitorDetails)

    Return dtVisitorDetails
End Function

解决方案

I changed

(txtIdNumber.Text) - 1)

to '0' in the line

txtFirstName.Text = CStr(dtVisitorDetails.Rows(CInt(CDbl(txtIdNumber.Text) - 1)).Item(1))

and the lines for lastName, company, etc.
So the line now reads

txtFirstName.Text = CStr(dtVisitorDetails.Rows(0).Item(1))

which has solved my problem. I thought that if I had '0' there that it would return row '0'(i.e. ID number 1) each time, but this is not the case as I am querying the ID number in my sql statement so it shows the ID number queried.


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

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