Wednesday, 15 April 2015

sql - C# Returning Data Objects Issue -



sql - C# Returning Data Objects Issue -

i trying homecoming info object database can access (for example) client id within asp.net website. upon client logging in object returned. however, getting error:

'invalid effort read when no info present.'

i have completed sql query on database (executing stored procedure) returns right information, know there. can presume there wrong next method:

using (sqlconnection sqlconn = new sqlconnection(configurationmanager.connectionstrings["connectionstring"].connectionstring)) { using (sqlcommand sqlcomm = new sqlcommand("select_customer_by_username_and_password", sqlconn)) { sqlcomm.connection.open(); seek { sqlcomm.commandtype = commandtype.storedprocedure; sqlcomm.parameters.add("@username", sqldbtype.nvarchar, 25).value = pusername; sqlcomm.parameters.add("@password", sqldbtype.nvarchar, 25).value = ppassword; using (sqldatareader sqldr = sqlcomm.executereader(commandbehavior.singlerow)) { if (sqldr.hasrows) { //creating new object returned using info database. homecoming new client { customerid = convert.toint32(sqldr["customerid"]) }; } else homecoming null; } } grab (exception) { throw; } { sqlcomm.connection.close(); } } }

you need phone call sqldr.read(), otherwise "record pointer" point record. hasrows indicates there rows can read. read each row (or first one), need phone call read 1 time or in while loop.

for example:

if (reader.hasrows) { while (reader.read()) ... }

your code should read:

using (sqldatareader sqldr = sqlcomm.executereader(commandbehavior.singlerow)) { if (sqldr.read()) { //creating new object returned using info database. homecoming new client { customerid = convert.toint32(sqldr["customerid"]) }; } else homecoming null; }

by way: congrats on using using , parameterized queries!

c# sql object

No comments:

Post a Comment