[英]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. 我正在尝试使用文本文件中的数据从数据库上传txt文件,并从数据库中检索数据。我之前工作正常,但突然出现此错误。
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";
}
Put a check, your sql statement might not bring the data always 进行检查,您的sql语句可能不会始终带来数据
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 还建议您在这种情况下使用DBNull.Value
ie 即
mticketRef = tblTemp.Rows[0]["t_reference"]!=DBNull.Value? tblTemp.Rows[0]["t_reference"].ToString():"";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.