sql - Error on C# when trying to fetch multiple data from database -
i have code in c#:
private void sqlconnlabel() { noidpenghuni = new sqlparameter(); sqlconnection con = new sqlconnection(strcon); com2 = new sqlcommand(); com2.connection = con; con.open(); com2.commandtype = commandtype.storedprocedure; com2.commandtext = "label"; noidpenghuni.sqldbtype = sqldbtype.varchar; noidpenghuni.size = 50; noidpenghuni.parametername = "@noidpenghuni"; noidpenghuni.value = noidpenghunic; noidpenghuni.direction = parameterdirection.input; com2.parameters.add(noidpenghuni); string namapenghuni; string jkpenghuni; string notelppenghuni; string alamatpenghuni; string nokamar; namapenghuni = convert.tostring(com2.executescalar()); jkpenghuni = convert.tostring(com2.executescalar()); notelppenghuni = convert.tostring(com2.executescalar()); alamatpenghuni = convert.tostring(com2.executescalar()); nokamar = convert.tostring(com2.executescalar()); sqldatareader reader = com2.executereader(); while (reader.read()) { namapenghuni = reader["namapenghuni"] == dbnull.value ? null : (string)reader["namapenghuni"]; jkpenghuni = reader["jkpenghuni"] == dbnull.value ? null : (string)reader["jkpenghuni"]; notelppenghuni = reader["notelppenghuni"] == dbnull.value ? null : (string)reader["notelppenghuni"]; alamatpenghuni = reader["alamatpenghuni"] == dbnull.value ? null : (string)reader["alamatpenghuni"]; nokamar = reader["nokamar"] == dbnull.value ? null : (string)reader["nokamar"]; } label9.text = noidpenghunic; label8.text = namapenghuni; if (jkpenghuni == "p") label7.text = "male"; else label7.text = "female"; label6.text = notelppenghuni; label18.text = alamatpenghuni; label5.text = nokamar; con.close(); }
when try run keeps telling me
indexoutofrangeexception unhandled.
i think data won't fetched c#. takes 'namapenghuni'
for example: if take data noidpenghuni='110801101, namapenghuni should priska hapsari, jkpenghuni should w, notelppenghuni should 08567711332, , alamatpenghuni should jl. mega cinere no. 29, cinere.
but on locals section can see string variables values priska hapsari.
what did wrong?
you call 5 times executescalar before executereader.
executescalar returns first column of first row (just 1 result).
calling 5 times results in same value 5 variables
the indexoutofrange exception caused following executereader expects find 5 columns in returned values, can't see if storedprocedure returns 5 values per row
Comments
Post a Comment