c# - Error: Invalid attempt to call Read when reader is closed after the while loop? -
hello have method reads info sql , saves them arrays.
to find out how many rows sql result has wrote this:
datatable dt = new datatable(); dt.load(rdr); count = dt.rows.count; after that, sqldatareader saves results arrays.
here finish code:
public bookingupdate[] getbookingupdates(string token) { string command = "select b.id,b.veranstalter, rr.von ,rr.bis, b.thema, b.storno, ra.bezeichnung buchung b bring together reservierungraum rr on rr.buchung_id = b.id bring together raum ra on ra.id = rr.raum_id b.update_date between dateadd (day , -20 , getdate()) , getdate() , b.bookvernr = 0"; sqlconnection connection = new sqlconnection(getconnectionstring()); bookingupdate[] bookingupdate = new bookingupdate[1]; connection.open(); seek { sqlcommand cmd = new sqlcommand(command, connection); sqldatareader rdr = null; int count = 0; int c = 0; rdr = cmd.executereader(); datatable dt = new datatable(); dt.load(rdr); count = dt.rows.count; bookingupdate = new bookingupdate[count]; while (rdr.read()) // <--- here comes error { bookingupdate[c] = new bookingupdate(); bookingupdate[c].bookingid = convert.toint32(rdr["id"]); bookingupdate[c].fullusername = rdr["veranstalter"].tostring(); bookingupdate[c].newstart = (datetime)rdr["von"]; bookingupdate[c].newend = (datetime)rdr["bis"]; bookingupdate[c].newsubject = rdr["thema"].tostring(); bookingupdate[c].newlocation = rdr["bezeichnung"].tostring(); if (rdr["storno"].tostring() != null) { bookingupdate[c].deleted = true; } else { bookingupdate[c].deleted = false; } c++; } } grab (exception ex) { log.error(ex.message + "\n\rstacktrace:\n\r" + ex.stacktrace); } { connection.close(); } homecoming bookingupdate; } the exeption : invalid effort phone call read when reader closed
the load-method closes datareader, hence next phone call read() fails (well, that's excatly exception tells you).
once read info datatable, query , utilize select projection create bookingupdate instances (no need while-loop/bookingupdate[]). code can trimmed downwards to
string command = "select b.id,b.veranstalter, rr.von ,rr.bis, b.thema, b.storno, ra.bezeichnung buchung b bring together reservierungraum rr on rr.buchung_id = b.id bring together raum ra on ra.id = rr.raum_id b.update_date between dateadd (day , -20 , getdate()) , getdate() , b.bookvernr = 0"; sqlcommand cmd = new sqlcommand(command, new sqlconnection(getconnectionstring())); connection.open(); datatable dt = new datatable(); dt.load(cmd.executereader()); var bookingupdate = dt.rows.oftype<datarow>().select (row => new bookingupdate { bookingid = convert.toint32(row["id"]), fullusername = row["veranstalter"].tostring(), newstart = (datetime)row["von"], newend = (datetime)row["bis"], newsubject = row["thema"].tostring(), newlocation = row["bezeichnung"].tostring(), deleted = row["storno"].tostring() != null // note line makes no sense. if can phone call `tostring` on object, not 'null' }).toarray(); homecoming bookingupdate; (note: omited try-block readability)
you may want datarowextensions, field method, create code more readable.
c# sql sqldatareader
No comments:
Post a Comment