Asp.net web服务页面System.IndexOutOfRangeException
这是我的服务代码页面Asp.net web服务页面System.IndexOutOfRangeException,asp.net,asmx,Asp.net,Asmx,这是我的服务代码页面 public class GetAreaFromCity : System.Web.Services.WebService { [WebMethod] public GetAreaByCityId ClassForGetCIty(int City_Id) { string CS =ConfigurationManager.ConnectionStrings["FOODINNConnectionString"].ConnectionSt
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 this parameter object with 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 & then polute this 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
}
}
}
这是一门按城市划分面积的课程
public class GetAreaByCityId
{
public int Ca_Id {get;set; }
public int City_Id { get; set; }
public int Area_Id { get; set; }
}
上面的数据库程序是可以检索数据的
System.IndexOutOfRangeException:城市Id
位于System.Data.ProviderBase.FieldNameLookup.GetOrdinal(字符串字段名)
位于System.Data.SqlClient.SqlDataReader.GetOrdinal(字符串名称)
位于System.Data.SqlClient.SqlDataReader.get_项(字符串名称)
在c:\Users\Mudassir\Documents\Visual Studio 2013\Projects\WebApplication1\WebAppli中的WebApplication1.GetAreaFromCity.ClassForGetCIty(Int32 City\u Id)中
上面的错误我不知道是什么问题您的存储过程只返回
区域Id
。“while循环”while(reader.Read()){
中的代码正在尝试从两列读取数据:
City\u Id
Area\u Id
City\u Id
添加到存储过程查询的结果集中,但您已经有了该值,因为您正在将其作为参数传递给存储过程
最简单的修复方法可能是更改这一行:
GETAreaByCityId.City\u Id=Convert.ToInt32(读卡器[“City\u Id]”);
为此:
GETAreaByCityId.City\u Id=City\u Id;
我很高兴它成功了!当你有机会时,请将我的答案标记为“接受”,这将使你的声誉提高+2,如果你认为这是一个“高质量”的答案,那么也请对其进行投票。ASMX是一项传统技术,不应用于新的开发。WCF或ASP.NET我们API应用于所有新开发的web服务客户端和服务器。提示:微软已经退出了MSDN。
Create Proc [dbo].[spGetCityById]
@ID int
as
Begin
Select Area_Id from
CITIES_AREA where City_Id = @ID
End
GO