使用Web服务索引超出范围错误 [英] index out of range error with web servise
本文介绍了使用Web服务索引超出范围错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public List<string> GetName(string CustName)
{
string sqlconnectionstring = ConfigurationManager.ConnectionStrings["sqlconn"].ConnectionString;
List<string> CustomerNames = new List<string>();
using (SqlConnection con = new SqlConnection(sqlconnectionstring))
{
SqlCommand cmd = new SqlCommand("Name",con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter para = new SqlParameter("@FName",CustName);
cmd.Parameters.Add(para);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
CustomerNames.Add(dr["Name"].ToString());
}
}
return CustomerNames;
}
完全出现。我想可能是存储过程..
At which it exactly occur.I think May be Stored procedure..
推荐答案
在while循环之前使用if(dr.hasRow)....
use if(dr.hasRow) before while loop....
基于错误
Based on the error
System.IndexOutOfRangeException: Name
at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
at System.Data.SqlClient.SqlDataReader.get_Item(String name)
at SuperMarket.SearchName.GetName(String CustName) in c:\Users\shrikesh\Desktop\shrikeshASP\SuperMarket\SuperMarket\SuperMarket\SearchName.asmx.cs:line 38
看起来问题出在行上
it looks like the problem is at row
CustomerNames.Add(dr["Name"].ToString());
很可能你的结果集中没有字段Name。使用调试器,在该行停止并查看与数据读取器关联的列。
Most likely you don't have a field "Name" in your result set. Using the debugger, stop at that row and have a look at the columns that are associated with the data reader.
这篇关于使用Web服务索引超出范围错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文