搞不明白了,请高手解答(在线等!)
这是我的一个Web服务的方法
[WebMethod]
public string Login(String userName, string password)
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings[ "connString "].ConnectionString);
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
string sqlLogin = "select [Name],Mobile from tb_user_Info where [Name]= ' " + userName + " 'and Pwd= ' " + password + " ' ";
try
{
SqlDataAdapter da = new SqlDataAdapter(sqlLogin, conn);
da.Fill(ds, "UserTable ");
int k = ds.Tables[ "UserTable "].Rows.Count;
if (k <= 0)
{
return "用户名或密码错误! ";
}
else
{
for(int i =0;i <=k;i++)
{
string tel = ds.Tables[ "UserTable "].Rows[i][ "Mobile "].ToString();
Session[ "Telphone "] = tel; //运行到此就出错误了,说什么未将对象引用设置到对象的实例,这是怎么回事啊????
}
}
}
catch (Exception ex)
{
return "用户名或密码错误! ";
}
return "登陆成功! ";
}
[解决办法]
加上这个试试~~
[ WebMethod(EnableSession=true) ]
[解决办法]
Session[ "Telphone "] = tel
tel 没赋值呀
[解决办法]
Session 是在Page里的
[解决办法]
ds.Tables[ "UserTable "].Rows[i][ "Mobile "].ToString(); 这里有问题
debug一下看看是不是null
[解决办法]
for(int i =0;i <=k;i++)
最终问题,i <=k;改成i <k;应该是index超范围了。