pretyv5
pretyv5

Reputation: 105

Getting error: "There is no row at position zero"

I am trying to upload an txt file with and retreive data from the database for the data which is in the text file.I was working fine before but suddenly this error shows up.

This is what there in text file....

X341HRK20140331             1000002                        
W177WAK20140405             1000004                        
R969GOJ20140405             1000005                        
W214VLR20140405             1000006  

When I load the text file it comes up with this error:

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

Upvotes: 0

Views: 313

Answers (1)

huMpty duMpty
huMpty duMpty

Reputation: 14460

Put a check, your sql statement might not bring the data always

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

Also recommend you to use DBNull.Value in this case

i.e.

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

Upvotes: 4

Related Questions