C# &引用;当不存在数据时读取的尝试无效”;使用SQLDataReader时

C# &引用;当不存在数据时读取的尝试无效”;使用SQLDataReader时,c#,asp.net,C#,Asp.net,以下是我的一般功能: 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 /// </sum

以下是我的一般功能:

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" ) );
            }
        }
    }

}
在我添加GetDistInfo方法之前,它工作得很好,登录到用户。然后我尝试添加会话变量和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时,它会正确地回调客户机名称。我不知道为什么它在代码中没有这样做。有人能看到我丢失了什么吗?

请尝试更换此。实际上,代码中缺少
Read()

using ( var reader = cmd.ExecuteReader() )
{
     return reader.GetString( reader.GetOrdinal( "Name" ) );
}
有以下几点

using ( var reader = cmd.ExecuteReader() )
{
     if(reader.Read())
     {
          return reader.GetString( reader.GetOrdinal( "Name" ) );
     }
     return null;
}
您需要先阅读(当您在代码中使用上面的读取器时):

编辑 对你所犯的错误作出反应;既然你定义了你的函数

public static string GetDistInfo ( int distID )

所有可能的“功能外”方法都必须返回某些内容。在您的情况下,您应该返回一个字符串,或者可能返回null,这取决于您想要执行的操作。

这似乎是朝着正确方向迈出的一步,但现在它给了我一个新错误:编译器
错误消息:CS0161:'GeneralFunctions.GetDistInfo(int)“:并非所有代码路径都返回值
Anyideas?
String。空的
将像巧克力一样占用内存。微优化@James,使用适合您需要的工具。@PankajGarg,阅读本文,我建议您检查其余的代码,而不是只定义一次的静态代码。。。你说的没有道理。我添加了返回空值,这就解决了它。你能帮助我理解为什么在读取总是返回结果的情况下,这是在寻找null吗?编译器注意到没有返回语句。编译器无法证明代码路径永远不会离开,因此它希望命中一个return语句。您是第一个发布return null解决方案的人,它工作得非常出色。必须将此作为答案进行检查。感谢大家的帮助和快速回复。
using ( var reader = cmd.ExecuteReader() )
{
    // you haven't positioned yourself on a record yet.
    while (reader.Read())
    {
        return reader.GetString( reader.GetOrdinal( "Name" ) );
    }
    return string.Empty;
}
public static string GetDistInfo ( int distID )