得到错误:"位置零处没有行"

时间:2014-04-10 16:19:13

标签: c# .net

我正在尝试上传一个txt文件,并从数据库中检索文本文件中的数据。我之前工作正常,但突然出现此错误。

这就是文本文件....

X341HRK20140331             1000002                        
W177WAK20140405             1000004                        
R969GOJ20140405             1000005                        
W214VLR20140405             1000006  

当我加载文本文件时,会出现此错误:

StreamReader reader = File.OpenText(ofd.FileName);

while ((line = reader.ReadLine()) != null && line.Trim().Length>0)
{
   //MUST STRIP ANY LEADING BLANK SPACES
   vrm = line.Substring(0, 7).Trim().Replace(" ",""); 
   eventYear = line.Substring(7, 4);
   eventMonth = line.Substring(11, 2);
   eventDay = line.Substring(13, 2);
   dateOfEvent = eventYear + "-" + eventMonth + "-" + eventDay;
                                enquirerReference = line.Substring(29, 6);
                                dateSettledString="";
                                vq_entry = "";

   //lookup-up other data from ICPS

   string stringCommand = "SELECT t_number, t_reference, t_zone_name, t_street_name, t_camera_ticket, t_date_finally_settled,te_event FROM tickets  inner join  ticket_events on tickets.t_number = ticket_events.te_system_ref WHERE t_number=@enquirerReference";
                                    SqlCommand command = new SqlCommand(stringCommand, connection);
                                    command.CommandTimeout = 900;
                                    command.Parameters.AddWithValue("@enquirerReference", enquirerReference);
                                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                                    DataTable tblTemp = new DataTable("DataSet");
                                    adapter.Fill(tblTemp);

      ticketRef = tblTemp.Rows[0]["t_reference"].ToString(); -- this is where I receive this error.                     
      site_zoneName = tblTemp.Rows[0]["t_zone_name"].ToString();
      site_streetName = tblTemp.Rows[0]["t_street_name"].ToString();
      issue_type = tblTemp.Rows[0]["t_camera_ticket"].ToString();
      issue_type = issue_type.Replace("-1", "Camera");
      issue_type = issue_type.Replace("0", "Manual");
      dateSettledString = tblTemp.Rows[0]["t_date_finally_settled"].ToString().Trim();
      events = tblTemp.Rows[0]["te_event"].ToString();
      excluded = "";

      //Read Ticket Exclusion File and process
      StreamReader readerTEF = File.OpenText(ticket_exclude_file);
      while ((lineTEF = readerTEF.ReadLine()) != null)
           {
              if (lineTEF == ticketRef)
              {
                  excluded=excluded + "Excluded on Ticket Reference";
              }
            }

       //Read ZoneName Exclusion File and process
       //Check on matching zonename and streetname
       StreamReader readerZNE = File.OpenText(zonename_exclude_file);
       while ((lineZNE = readerZNE.ReadLine()) != null)
        {
           testSite=lineZNE.Split('#');
           if (testSite[0] == site_zoneName && testSite[1] == site_streetName)
              {
                  excluded = excluded + "Excluded on Zone & Street name";
               }
        }


        //VOID VRM
       if (vrm.Trim().ToUpper() == "VOID")
         {
              excluded = excluded + "Excluded on VRM";
         }


        DateTime dtEvent = DateTime.Parse(dateOfEvent);
        TimeSpan span = DateTime.Now - dtEvent;
        days_past_event = span.Days;

         if (dateSettledString.Length > 5)
         {
            excluded = excluded + "Ticket Has Already Been Settled";
         }

1 个答案:

答案 0 :(得分:4)

检查一下,你的sql语句可能永远不会带来数据

if(tblTemp !=null)
{
    if(tblTemp.Rows.Count>0)
    {
        mticketRef = tblTemp.Rows[0]["t_reference"].ToString();
    }
}

还建议您在这种情况下使用DBNull.Value

即。

mticketRef = tblTemp.Rows[0]["t_reference"]!=DBNull.Value? tblTemp.Rows[0]["t_reference"].ToString():"";