There is no row at position 0

自古美人都是妖i 提交于 2021-02-07 20:22:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!