System.Web.Services.Protocols.SoapException: 服务器无法处理请求。 ---> System.NullReferenceException: 未将对象引用设置到对象的实例。
在 Service.Login(String username, String pwd)
--- 内部异常堆栈跟踪的结尾 ---
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data.SqlClient;
using System.Data;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public int Login(string username,string pwd)
{
DataRow dr= LuhooLogin(username, pwd);
if (dr != null)
{
Session.Add("l_Name", dr["Id"].ToString());
Session.Add("l_id", dr["UserName"].ToString());
return 1;
}
else {
return 0;
}
}
private DataRow LuhooLogin(string name, string pwd) {
pwd = MD5(pwd, true);
string sql = string.Format("select * from xys_sys_User where UserName='{0}' and UserPassword ='{1}'",name,pwd);
DataSet ds= FillDS(sql);
if (ds == null) {
return null;
}
if (ds.Tables[0].Rows.Count > 0)
{
return ds.Tables[0].Rows[0];
}
else {
return null;
}
}
private string MD5(string Input, bool Half)
{
string output = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Input, "MD5").ToLower();
if (Half)//16位MD5加密(取32位加密的9~25字符)
output = output.Substring(8, 16);
return output;
}
private SqlConnection getConn()
{
try
{
String connString;
connString = "server=.;uid=xys;pwd=xuey;database=XYSCMS;";
SqlConnection myConnection1 = new SqlConnection(connString);
return myConnection1;
}
catch (Exception e)
{
// Display the error.
return null;
Console.WriteLine(e.ToString());
}
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sql"></param>
/// <returns>datase</returns>
private DataSet FillDS(string sql)
{
SqlConnection conn = getConn();
try
{
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter(sql, conn);
ad.Fill(ds);
return ds;
}
catch
{
}
finally
{
conn.Close();
}
return null;
}
}
调用
Service FlashNetLogin = new Service();
int i = FlashNetLogin.Login("kke_007", "646516");