这是我的通用功能:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// Summary description for GeneralFunctions
/// </summary>
public class GeneralFunctions
{
public GeneralFunctions ()
{
//
// TODO: Add constructor logic here
//
}
public static DataTable GetData ( string query )
{
SqlDataAdapter dataAdapter;
DataTable table;
try
{
dataAdapter = new SqlDataAdapter( query, GetConnectionString() );
table = new DataTable();
dataAdapter.Fill( table );
return table;
}
catch ( Exception ex )
{
}
finally
{
dataAdapter = null;
table = null;
}
return table;
}
private static string GetConnectionString ()
{
string connectionString = ConfigurationManager.ConnectionStrings[ "CAPortalConnectionString" ].ConnectionString;
return connectionString;
}
public static int? AuthenticateLogin ( string username, string password )
{
using ( var conn = new SqlConnection( GetConnectionString() ) )
using ( var cmd = conn.CreateCommand() )
{
conn.Open();
cmd.CommandText =
@"SELECT
DistID
FROM
Distributor
WHERE
Username = @username
AND
Password = @password";
cmd.Parameters.AddWithValue( "@username", username );
cmd.Parameters.AddWithValue( "@password", password );
using ( var reader = cmd.ExecuteReader() )
{
if ( !reader.Read() )
{
// no results found
return null;
}
return reader.GetInt32( reader.GetOrdinal( "DistID" ) );
}
}
}
public static string GetDistInfo ( int distID )
{
using ( var conn = new SqlConnection( GetConnectionString() ) )
using ( var cmd = conn.CreateCommand() )
{
conn.Open();
cmd.CommandText =
@"SELECT
FName + ' ' + LName AS Name
FROM
Distributor
WHERE
DistID = @distid";
cmd.Parameters.AddWithValue( "@distid", distID );
using ( var reader = cmd.ExecuteReader() )
{
return reader.GetString( reader.GetOrdinal( "Name" ) );
}
}
}
}
这是我的登录页面:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class index : System.Web.UI.Page
{
protected void Page_Load ( object sender, EventArgs e )
{
}
protected void but_login_Click ( object sender, EventArgs e )
{
if ( username_id.Text != "" || password.Text != "" )
{
// Verify the username and password match the database
var distId = GeneralFunctions.AuthenticateLogin( username_id.Text, password.Text );
if ( distId != null )
{
// User is authenticated
var name = GeneralFunctions.GetDistInfo( (int)distId );
Session[ "DistName" ] = name;
Session[ "DistID" ] = distId;
Response.Redirect( "dashboard.aspx", false );
}
else
{
// provide error label here username and password do not match
authentFailed.Text = "Username / Password did not match our records";
}
}
else
{
// Username or Password blank error lable
authentFailed.Text = "Please Input Username / Password";
}
}
}
在我添加 GetDistInfo 方法之前,它工作得很好,登录了用户。然后我尝试添加 Session 变量和 GetDistInfo 方法。我将从 AuthenticateLogin 返回的 DistID 传递给 GetDistInfo 方法。它出现以下错误:
异常详细信息:System.InvalidOperationException:不存在数据时尝试读取无效。
Source Error:
Line 95: using ( var reader = cmd.ExecuteReader() )
Line 96: {
Line 97: return reader.GetString( reader.GetOrdinal( "Name" ) );
Line 98: }
Line 99: }
Source File: c:\inetpub\wwwroot\Base\ClientAccessPortal\App_Code\GeneralFunctions.cs Line: 97
当我对数据库运行 SQL 时,它正确地拉回了客户端名称。我不确定为什么它不在代码中这样做。任何人都能够看到我错过了什么?