mysql - Strange issue when retrieving data from my database -
when reading data table, 1 of numerical values returns -1, rather value given user.
private sub form1_load(sender object, e eventargs) handles mybase.load try dim conn new mysqlconnection("server=localhost;user=root;database=new_project;port=3306;password=********;") conn.open() dim command new mysqlcommand("select * students;", conn) dim dataset new dataset() dim dataadapter new mysqldataadapter() dataadapter.selectcommand = command dataadapter.fill(dataset, "students") dim datatable datatable = dataset.tables("students") x integer = 0 datatable.rows.count - 1 dim newstudent new student newstudent.intidnum = datatable.rows(x).item("idnumber") newstudent.strfirstname = datatable.rows(x).item("firstname") newstudent.strlastname = datatable.rows(x).item("lastname") newstudent.chrgender = datatable.rows(x).item("gender") newstudent.datedob = datatable.rows(x).item("dateofbirth") newstudent.intage = cint(today.year - newstudent.datedob.year) newstudent.intyeargroup = datatable.rows(x).item("yeargroup") newstudent.intskilllevel = datatable.rows(x).item("skilllevel") studentlist.add(newstudent) lststudents.items.add(newstudent.nameconcat(newstudent.strfirstname, newstudent.strlastname)) next conn.close() catch ex exception msgbox("error: " & ex.tostring()) end try end sub
when reading value "skilllevel", should read between 1 , 4, instead reads -1. hasn't occurred data added directly database through mysql, when using method in program, seems issue appears.
dim newstudent new student newstudent.strfirstname = txtforename.text newstudent.strlastname = txtsurname.text newstudent.datedob = dtpstudentdob.value if rdbmale.checked = true newstudent.chrgender = "m" else newstudent.chrgender = "f" end if newstudent.intage = newstudent.calcage(dtpstudentdob.value) newstudent.intskilllevel = cmbskilllevel.selecteditem newstudent.intyeargroup = cmbyeargroup.selecteditem newstudent.intidnum = cint(txtstudentid.text) dim conn new mysqlconnection("server=localhost;user=root;database=new_project;port=3306;password=********;") try conn.open() dim command mysqlcommand = new mysqlcommand command.connection = conn command.commandtext = "insert students values(@idnumber, @firstname, @lastname, @gender, @dateofbirth, @yeargroup, @skilllevel);" command.prepare() command.parameters.addwithvalue("@idnumber", newstudent.intidnum) command.parameters.addwithvalue("@firstname", newstudent.strfirstname) command.parameters.addwithvalue("@lastname", newstudent.strlastname) command.parameters.addwithvalue("@gender", newstudent.chrgender) command.parameters.addwithvalue("@dateofbirth", newstudent.datedob) command.parameters.addwithvalue("@yeargroup", newstudent.intyeargroup) command.parameters.addwithvalue("@skilllevel", newstudent.intskilllevel) command.executenonquery() catch ex exception msgbox("error: " & ex.tostring()) conn.close() end try
this section of code student added system. causing error appear?
for reference, main form add in data uses enumeration:
private enum skilllevels integer 1 = 1 2 = 2 3 = 3 4 = 4 end enum
and link dropdown: cmbskilllevel.datasource = system.enum.getvalues(gettype(skilllevels))
the obvious possibility no selection made in skill-level dropdown , why getting -1, can check if selection has been made in dropdown ensuring not -1
for example skills drop down may this
<select id="skilllevel"> <option value="0" selected="selected">please select</option> <option value="1">low</option> <option value="2">medium</option> <option value="3">high</option> <option value="4">very-high</option> </select>
if no selection made default value returned 0 (0) , can check valid values looking returned values > 0
as far can remember if omit selected="selected" returned value -1
Comments
Post a Comment