J.Nandi
J.Nandi

Reputation: 11

sql,vb error "no row at position 0"

I get the message "there is no row at position 0" in the form with the following code:

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)

Error highlights the bold line.

Upvotes: 0

Views: 403

Answers (2)

Nagaraj S
Nagaraj S

Reputation: 13484

Try like this

   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.....

Upvotes: 0

Al-3sli
Al-3sli

Reputation: 2181

your code missing fill for your dataTable

Try this :

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

Upvotes: 1

Related Questions