c# - IndexOutOfRange unless I split the query into multiple queries -


i've written sql query c# program when try run query error

system.indexoutofrangeexception:

i tried alter query order see if 1 doing it, , noticed gave me error when had code in while (drorder.read()) tried convert @ least 2 of these 3 columns (adres, lev, taal).

// code gives error     sqlcommand getlist = new sqlcommand("select * besw best = @best", connectie.connmevo); getlist.parameters.add("@best", sqldbtype.varchar).value = data.corrigeerbestnr;  drorder = getlist.executereader();  while (drorder.read()) {     datetimepicker1.value = convert.todatetime(drorder["bestel"]);     datetimepicker2.value = convert.todatetime(drorder["plan"]);     combobox2.text = drorder["adres"].tostring();     combobox1.text = drorder["lev"].tostring();     textbox8.text = drorder["taal"].tostring(); } 

however when split query 3 near identical queries each 1 of 3 columns give error, works without issue.

// code im using without error     sqlcommand getlist = new sqlcommand("select bestel, [plan], adres besw best = @best", connectie.connmevo); getlist.parameters.add("@best", sqldbtype.varchar).value = data.corrigeerbestnr;  drorder = getlist.executereader();  while (drorder.read()) {     datetimepicker1.value = convert.todatetime(drorder["bestel"]);     datetimepicker2.value = convert.todatetime(drorder["plan"]);     combobox2.text = drorder["adres"].tostring(); }  sqlcommand getlist2 = new sqlcommand("select lev besw best = @best", connectie.connmevo); getlist2.parameters.add("@best", sqldbtype.varchar).value = data.corrigeerbestnr;  drorder = getlist2.executereader();  while (drorder.read()) {     combobox1.text = drorder["lev"].tostring(); }  sqlcommand getlist3 = new sqlcommand("select taal besw best = @best", connectie.connmevo); getlist3.parameters.add("@best", sqldbtype.varchar).value = data.corrigeerbestnr;  drorder = getlist3.executereader();  while (drorder.read()) {     textbox8.text = drorder["taal"].tostring(); } 

i have no idea why this, since other queries in program work, have queries read entire table , don't give problems having fields in while loop.

now question is, why 1 of these code blocks work while other gives error? , perhaps if 1 knows solution i'd hear it, since feel it's better have 1 query.

i know forgot put dispose of queries in these code blocks.

additional information problem: when run single query code column gives error lev if change order of columns, problem given second column listed out of these 3 (adres, lev, taal).

edit: "new" code (db has 28 columns)

// code gives error     sqlcommand getlist = new sqlcommand("select * besw best = @best", connectie.connmevo); getlist.parameters.add("@best", sqldbtype.varchar).value = data.corrigeerbestnr;  drorder = getlist.executereader();  while (drorder.read()) {     if (!drorder.isdbnull(10)) { datetimepicker1.value = convert.todatetime(drorder[10]); }     if (!drorder.isdbnull(11)) { datetimepicker2.value = convert.todatetime(drorder[11]); }     if (!drorder.isdbnull(7)) { combobox1.text = drorder[7].tostring(); }     if (!drorder.isdbnull(8)) { combobox2.text = drorder[8].tostring(); }     if (!drorder.isdbnull(25)) { textbox8.text = drorder[25].tostring(); } } 

this query

select bestel,[plan],adres besw best=@best 

returns 3 columns, , can't take drorder["lev"], drorder["taal"]

include taal , lev in 1st select

sqlcommand getlist = new sqlcommand("select bestel,[plan],adres, lev, taal besw best=@best", connectie.connmevo); 

update

indexoutofrangeexception means "no column specified name found."

try take value index

combobox1.text = drorder[3].tostring(); textbox8.text = drorder[4].tostring(); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -