vb.net - IndexOutOfRangeException was Unhandled. Cannot find table 0 -
can please on how fix error? im getting error "error:syntax error(missing operator)in query expression '''ans'." code:
imports system.data imports system.data.oledb public class frmexam : inherits system.windows.forms.form dim ds new dataset() dim qno() integer dim pos integer private sub btnfinish_click(byval sender system.object, byval e system.eventargs) handles btnfinish.click processanswer() dim i, marks integer dim dr datarow marks = 0 = 0 noq - 1 dr = ds.tables(0).rows(qno(i)) if not isdbnull(dr.item("ans")) andalso dr.item("correctans") = dr.item("ans") marks += 1 end if next try con.open() dim cmd new oledbcommand("insert stud values(stud.nextval,'" & subjectcode & "','" & username & "','" & marks & ")", con) msgbox(cmd.commandtext) cmd.executenonquery() dim msg string msg = "username : " & username & controlchars.crlf & _ "subject : " & subjectcode & controlchars.crlf & _ "total questions : " & noq & controlchars.crlf & _ "marks : " & marks msgbox(msg, "result") catch ex exception msgbox(ex.message) if con.state <> connectionstate.closed con.close() end if end try me.dispose() end sub private sub frmexam_load(byval sender system.object, byval e system.eventargs) handles mybase.load redim qno(noq) try dim da new oledbdataadapter("select question, ans1, ans2, ans3, ans4, correctans, qtype, '' ans question subjectcode = " & subjectcode, con) da.fill(ds, "question") catch ex exception msgbox("error:" & ex.message) me.dispose() end try randomize() dim totrows integer totrows = ds.tables(0).rows.count dim i, r, j integer dim present boolean = 0 while < 5 r = cint((totrows - 1) * rnd()) present = false j = 0 if r = qno(j) present = true exit end if next if not present qno(i) = r = + 1 end if loop pos = 0 displayquestion() end sub sub displayquestion() dim row datarow lblqno.text = str(pos + 1) & "/" & noq lblsubcode.text = "subject : " & subjectcode row = ds.tables(0).rows(qno(pos)) txtquestion.text = row.item(0) txtans1.text = row.item(1) txtans2.text = row.item(2) txtans3.text = row.item(3) txtans4.text = row.item(4) lblqno2.text = str(pos + 1) & "/" & noq lblsubcode2.text = "subject:" & subjectcode row = ds.tables(0).rows(qno(pos)) txtquestion2.text = row.item(0) txtanstrue.text = row.item(1) txtansfalse.text = row.item(2) lblqno3.text = str(pos + 1) & "/" & noq lblsubcode.text = "subject : " & subjectcode row = ds.tables(0).rows(qno(pos)) txtquestion3.text = row.item(1) txtans.text = row.item(2) end sub private sub btnnext_click(byval sender system.object, byval e system.eventargs) handles btnnext.click processanswer() if pos < noq - 1 pos = pos + 1 displayquestion() else beep() end if end sub private sub btnprev_click(byval sender system.object, byval e system.eventargs) handles btnprev.click processanswer() if pos > 0 pos = pos - 1 displayquestion() end if end sub public sub processanswer() dim row datarow dim ans string = "" row = ds.tables(0).rows(qno(pos)) if rdbtnans1.checked ans = "1" end if if rdbtnans2.checked ans = "2" end if if rdbtnans3.checked ans = "3" end if if rdbtnans4.checked ans = "4" end if if rdbtntrue.checked ans = "1" end if if rdbtnfalse.checked ans = "2" end if if txtans.text = "" ans = "txtans" end if ds.tables(0).rows(qno(pos)).item("ans1") = ans end sub private sub btnprev2_click(byval sender system.object, byval e system.eventargs) handles btnprev2.click processanswer() if pos > 0 pos = pos - 1 displayquestion() end if end sub private sub btnnext2_click(byval sender system.object, byval e system.eventargs) handles btnnext2.click processanswer() if pos < noq - 1 pos = pos + 1 displayquestion() else beep() end if end sub private sub btnprev3_click(byval sender system.object, byval e system.eventargs) handles btnprev3.click processanswer() if pos > 0 pos = pos - 1 displayquestion() end if end sub private sub btnnext3_click(byval sender system.object, byval e system.eventargs) handles btnnext3.click processanswer() if pos < noq - 1 pos = pos + 1 displayquestion() else beep() end if end sub end class
this highlighted texxt:totrows = ds.tables(0).rows.count
when want use alias field expression, access requires use as
keyword.
this fail ...
select question, ans1, ans2, ans3, ans4, correctans, qtype, '' ans question
this could work ...
select question, ans1, ans2, ans3, ans4, correctans, qtype, '' ans question
however, uneasy field name matched table name. if can't rename field, alias table, , qualify fields table alias.
select q.question, q.ans1, q.ans2, q.ans3, q.ans4, q.correctans, q.qtype, '' ans question q
Comments
Post a Comment