sql,vb error"位置0和#34没有行;

时间:2013-12-30 09:04:34

标签: sql vb.net

我在表单中收到消息“位置0处没有行”,其中包含以下代码:

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    Dim i As Integer
    Dim xid As Integer
    Dim xQTY(0) As Integer
    Dim xQTY_ID(0) As Integer
    Dim xCount As Integer
    Dim xCounter_ID(0) As Integer
    'Dim sqlstrx(5) As String
    xid = 0
    **If lstitems.Items.Count > 0 Then**
        If Split(Me.Text, " - ")(1) = "Add" Then
            sqlSTR = "INSERT INTO orders (CustID, Cust_Name, order_date) " & _
                     "VALUES (" & txtcustid.Text & ", " _
                            & "'" & txtcustname.Text & "', " _
                            & "'" & Format(dttoday.Value, "MM/dd/yyyy") & "')"
            ExecuteSQLQuery(sqlSTR)
            sqlSTR = "SELECT * FROM orders ORDER BY order_no DESC"
            ExecuteSQLQuery(sqlSTR)
            xid = sqlDT.Rows(0)("order_no")
            For i = 0 To lstitems.Items.Count - 1
                sqlSTR = "INSERT INTO orders_detail (order_no, idDrug, DrugName, Unit_Cost, qty, totalcost) " & _
                         "VALUES (" & xid & ", " _
                                    & lstitems.Items(i).Text & ", " _
                              & "'" & lstitems.Items(i).SubItems(1).Text & "', " _
                              & "'" & lstitems.Items(i).SubItems(2).Text & "', " _
                                    & lstitems.Items(i).SubItems(3).Text & ", " _
                                    & lstitems.Items(i).SubItems(4).Text & ", " _
                                    & lstitems.Items(i).SubItems(5).Text & ")"
                ExecuteSQLQuery(sqlSTR)

错误突出显示粗线。

2 个答案:

答案 0 :(得分:1)

您的代码缺少对您的dataTable的填充

试试这个:

        Dim sqlAdapter As New SqlDataAdapter(sqlSTR, yourconnctionstring)
        sqlAdapter.Fill(sqlDT)
        If sqlDT.Rows.Count > 0 Then
        xid = sqlDT.Rows(0)("order_no")
        End If

答案 1 :(得分:0)

试试这个

   Dim sqlAdapter As New SqlDataAdapter(sqlSTR, yourconnctionstring)
    sqlAdapter.Fill(sqlDT)
    If sqlDT.Rows.Count > 0 Then
    xid = sqlDT.Rows(0)("order_no")
    End If
            'write your code here.....