Webservice上的IndexOutOfRangeException

时间:2014-10-02 23:38:09

标签: c# sql web-services asmx indexoutofboundsexception

我在网络服务上获得了IndexOutOfRangeException

[WebMethod]
public Svar FindAgent(int agentCode)
{
    SqlConnection conn = new SqlConnection(@"data source = BABU110-PC; integrated security = true; database = SecretAgentDB"); //forbindelse
    // Vi burde lave noget try catch

    conn.Open();
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = @"select * from Agents where Code = " + agentCode;
    SqlDataReader rdr = cmd.ExecuteReader();
    rdr.Read();

    Agent a = new Agent();
    a.Code = (int)rdr["Code"];
    a.Name = rdr["Name"].ToString();
    rdr.Close();

    cmd.CommandText = @"select * from AgentLicense where AgentCode = " + a.Code;
    rdr = cmd.ExecuteReader(); // execute vores sql
    // Hvis der er flere skal der laves en løkke her
    // Read() giver true tilbage, hvis der var flere data.
    // Kan fx bruges i en while 
    rdr.Read();
    License l = new License();
    l.Id = (int)rdr["Id"];
    l.Name = rdr["Name"].ToString(); ------ GIVES ME THE IndexOutOfRangeException HERE
    //
    rdr.Close();

    Svar s = new Svar();
    s.A = a;
    s.L = l;

    return s;
}

我在l.Name = rdr["Name"].ToString();

收到此错误
  

System.IndexOutOfRangeException:名称   SecretAgentServiceCS4.asmx.cs:第46行,

位于l.Name = rdr["Name"].ToString();所在的位置

0 个答案:

没有答案