问题
cmd.CommandText = "select * from product where prod_code='" & Trim(txtprod_code.Text) & "' and branch='" & w_location & "' and avail_stock <>" & (0) & ""
cmd.CommandType = CommandType.Text
con.Open()
da_uqc.SelectCommand = cmd
cmd.Connection = con
da_uqc.Fill(ds_uqc)
m_qty = ds_uqc.Tables(0).Rows(0)(4) 'error
da_uqc.Dispose()
ds_uqc.Dispose()
cmd.Dispose()
Is it possible to give like this m_qty = ds_uqc.Tables(0).Rows(0)(4)
?
回答1:
You're getting a run-time error denoting that there was no rows at all in the table since your query string does not get any matching rows, you may check rows count first:
If ds_uqc.Tables(0).Rows.Count > 0 then
m_qty = ds_uqc.Tables(0).Rows(0)(4)
End If
P.S: comments in VB.Net, starts by '
and not the C#.Net one //
.
回答2:
That tells you that no rows were loaded, presumably because there was no matching product/etc. This could be a case-sensitivity issue, or there could genuinely be no such products.
For info, using concatenation of inputs IS BAD. You should always prefer a parameterised command, both to avoid data-errors (when you get a quote in a name) but more importantly to avoid SQL injection.
来源:https://stackoverflow.com/questions/6556171/there-is-no-row-at-position-0