asp.net - web service page System.IndexOutOfRangeException -
public class getareafromcity : system.web.services.webservice { [webmethod] public getareabycityid classforgetcity(int city_id) { string cs =configurationmanager.connectionstrings["foodinnconnectionstring"].connectionstring; using (sqlconnection con = new sqlconnection(cs)) { sqlcommand cmd = new sqlcommand("spgetcitybyid",con); cmd.commandtype = commandtype.storedprocedure; sqlparameter parameter = new sqlparameter("@id", city_id); //to assiate parameter object cmd object cmd.parameters.add(parameter); getareabycityid getareabycityid =new getareabycityid(); con.open(); sqldatareader reader = cmd.executereader(); //as weakreference read data wewant tostring retrive column value & polute property city_id values while (reader.read()){ getareabycityid.city_id = convert.toint32(reader["city_id"]); getareabycityid.area_id = convert.toint32(reader["area_id"]); } return getareabycityid; //tostring return sql } } }
that's codes service page
public class getareabycityid { public int ca_id {get;set; } public int city_id { get; set; } public int area_id { get; set; } }
that's class getting area city
create proc [dbo].[spgetcitybyid] @id int begin select area_id cities_area city_id = @id end go
and above database procedure data can retrieve
system.indexoutofrangeexception: city_id @ system.data.providerbase.fieldnamelookup.getordinal(string fieldname) @ system.data.sqlclient.sqldatareader.getordinal(string name) @ system.data.sqlclient.sqldatareader.get_item(string name) @ webapplication1.getareafromcity.classforgetcity(int32 city_id) in c:\users\mudassir\documents\visual studio 2013\projects\webapplication1\webappli
the above error dont know whats problem
your stored procedure returning area_id
. code in "while loop" while (reader.read()){
attempting read data 2 columns:
city_id
area_id
you add column city_id
result set stored procedure query, have value because passing stored procedure parameter.
easiest fix change line:
getareabycityid.city_id = convert.toint32(reader["city_id"]);
to this:
getareabycityid.city_id = city_id;
Comments
Post a Comment