Index out of range. Can't get selectedIndexChanged as index for query












0















I get an 'index out of range error' however do not no why,



I have gridview master control and am using SelectedIndexChanged to display more information on the selected row in a details view, however when I try to point to the selected row in the query it gives an index out of range error.



I am using a ADO.NET Entity model and want to essentially retrieve data about the selected gridview row using another database table called EventLog and populate the details view with that.




Error Message (Line 50)



Line 50: description =
Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();



Line 51: resultCode =
Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().ResultCode);




Here is my SelectedIndexChanged code:



<!-- language: lang-c# -->
string description;
int resultCode;
int TaskInstanceID;
string Eid;
int IntEid;

//added 's' at end of eventDetailsView due to conflicting naming conventions
DetailsView eventDetailsViews = new DetailsView();

public void tasksGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
eventDetailsView.Visible = true;
//Get's the currently selected row
GridViewRow row = tasksGridView.SelectedRow;
selectedId = row.Cells[1].Text;
System.Diagnostics.Debug.WriteLine(selectedId);

Eid = tasksGridView.SelectedRow.Cells[1].Text;
IntEid = Convert.ToInt32(Eid);

description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();
*///description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc;*
resultCode = Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().ResultCode);
TaskInstanceID = Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().TaskInstanceID);

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Eid", typeof(int)),
new DataColumn("Description", typeof(string)),
new DataColumn("TaskInstanceID", typeof(int))
});
dt.Rows.Add(Eid, description, TaskInstanceID);

eventDetailsViews.DataSource = dt;
eventDetailsView.DataBind();

}


Here is my query code:



    protected void Page_Load(object sender, EventArgs e)
{
infordevEntitiesOrbis dbContext = new infordevEntitiesOrbis();
//Acting as a using SqlConnection to the database
using (infordevEntitiesOrbis context = new infordevEntitiesOrbis())
{
//On pageload, not on user interaction (Occurs regardless)
if (!IsPostBack)
{
d = d.AddMonths(-3);

//Query written using LINQ
Tquery = (from tasks in context.Tasks.AsEnumerable()
select new Task()
{
TaskID = tasks.TaskID,
TaskName = tasks.TaskName,
TaskPath = tasks.TaskPath,
Schedules = tasks.Schedules
}).ToList();


ASP Code for gridview and details view:



 <asp:GridView ID="tasksGridView" runat="server" DataKeyNames="TaskID" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" AutoGenerateColumns="False" AutoGenerateSelectButton="True" OnSelectedIndexChanged="tasksGridView_SelectedIndexChanged" OnSelectedIndexChanging="tasksGridView_SelectedIndexChanging" OnRowDataBound="tasksGridView_RowDataBound1" HorizontalAlign="Center">
<Columns>
<asp:BoundField DataField="TaskID" HeaderText="TaskID" ReadOnly="True" SortExpression="TaskID"></asp:BoundField>
<asp:BoundField DataField="TaskPath" HeaderText="TaskPath" SortExpression="TaskPath"></asp:BoundField>
<asp:BoundField DataField="TaskName" HeaderText="TaskName" SortExpression="TaskName"></asp:BoundField>
<asp:TemplateField HeaderText="RunTime">
<ItemTemplate>
<asp:TextBox ID="UTCRT" runat="server" Enabled="false" BorderStyle="Dotted" BorderWidth="1px" ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="UTCLRT" runat="server" Enabled="false" BorderStyle="Dotted" BorderWidth="1px" ReadOnly="True"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black"></FooterStyle>

<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White"></HeaderStyle>

<PagerStyle HorizontalAlign="Right" BackColor="White" ForeColor="Black"></PagerStyle>

<SelectedRowStyle BackColor="#6699FF" Font-Bold="True" ForeColor="White"></SelectedRowStyle>

<SortedAscendingCellStyle BackColor="#F7F7F7"></SortedAscendingCellStyle>

<SortedAscendingHeaderStyle BackColor="#4B4B4B"></SortedAscendingHeaderStyle>

<SortedDescendingCellStyle BackColor="#E5E5E5"></SortedDescendingCellStyle>

<SortedDescendingHeaderStyle BackColor="#242121"></SortedDescendingHeaderStyle>
</asp:GridView>


<asp:DetailsView ID="eventDetailsView" runat="server" AutoGenerateRows="False">
<RowStyle HorizontalAlign="Center" />
</asp:DetailsView>


RowDataBound:



foreach (GridViewRow row in tasksGridView.Rows)
{
currentDataIndex = e.Row.RowIndex;
if (row.RowType == DataControlRowType.DataRow)
{
if (utcRnTime != null)
{
try
{
utcLRTUpdate = Tquery[currentDataIndex].Schedules.FirstOrDefault().UtcLastRunTime.Value;
utcRTUpdate = Tquery[currentDataIndex].Schedules.FirstOrDefault().UtcRunTime.Value;


Any help would be appreciated!
I don't think any more code is required.










share|improve this question




















  • 3





    However you should provide where you get that error.

    – HimBromBeere
    Nov 22 '18 at 15:37











  • Have you checked if your methods has the correct items in it? e.g, when browsing though the cells if the count is greater than the number you are querying?

    – mahlatse
    Nov 22 '18 at 15:39











  • You do not check if IntEid = Convert.ToInt32(Eid); is not out of range

    – krzyski
    Nov 22 '18 at 15:43













  • No I havent checked if IntEid is out of range however I know it isnt, for example If I click on row 7 the table has more rows then 7.

    – Boywithanafro
    Nov 22 '18 at 15:45











  • I feel as though it has something to do with .FirstOrDefault()

    – Boywithanafro
    Nov 22 '18 at 15:45
















0















I get an 'index out of range error' however do not no why,



I have gridview master control and am using SelectedIndexChanged to display more information on the selected row in a details view, however when I try to point to the selected row in the query it gives an index out of range error.



I am using a ADO.NET Entity model and want to essentially retrieve data about the selected gridview row using another database table called EventLog and populate the details view with that.




Error Message (Line 50)



Line 50: description =
Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();



Line 51: resultCode =
Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().ResultCode);




Here is my SelectedIndexChanged code:



<!-- language: lang-c# -->
string description;
int resultCode;
int TaskInstanceID;
string Eid;
int IntEid;

//added 's' at end of eventDetailsView due to conflicting naming conventions
DetailsView eventDetailsViews = new DetailsView();

public void tasksGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
eventDetailsView.Visible = true;
//Get's the currently selected row
GridViewRow row = tasksGridView.SelectedRow;
selectedId = row.Cells[1].Text;
System.Diagnostics.Debug.WriteLine(selectedId);

Eid = tasksGridView.SelectedRow.Cells[1].Text;
IntEid = Convert.ToInt32(Eid);

description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();
*///description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc;*
resultCode = Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().ResultCode);
TaskInstanceID = Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().TaskInstanceID);

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Eid", typeof(int)),
new DataColumn("Description", typeof(string)),
new DataColumn("TaskInstanceID", typeof(int))
});
dt.Rows.Add(Eid, description, TaskInstanceID);

eventDetailsViews.DataSource = dt;
eventDetailsView.DataBind();

}


Here is my query code:



    protected void Page_Load(object sender, EventArgs e)
{
infordevEntitiesOrbis dbContext = new infordevEntitiesOrbis();
//Acting as a using SqlConnection to the database
using (infordevEntitiesOrbis context = new infordevEntitiesOrbis())
{
//On pageload, not on user interaction (Occurs regardless)
if (!IsPostBack)
{
d = d.AddMonths(-3);

//Query written using LINQ
Tquery = (from tasks in context.Tasks.AsEnumerable()
select new Task()
{
TaskID = tasks.TaskID,
TaskName = tasks.TaskName,
TaskPath = tasks.TaskPath,
Schedules = tasks.Schedules
}).ToList();


ASP Code for gridview and details view:



 <asp:GridView ID="tasksGridView" runat="server" DataKeyNames="TaskID" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" AutoGenerateColumns="False" AutoGenerateSelectButton="True" OnSelectedIndexChanged="tasksGridView_SelectedIndexChanged" OnSelectedIndexChanging="tasksGridView_SelectedIndexChanging" OnRowDataBound="tasksGridView_RowDataBound1" HorizontalAlign="Center">
<Columns>
<asp:BoundField DataField="TaskID" HeaderText="TaskID" ReadOnly="True" SortExpression="TaskID"></asp:BoundField>
<asp:BoundField DataField="TaskPath" HeaderText="TaskPath" SortExpression="TaskPath"></asp:BoundField>
<asp:BoundField DataField="TaskName" HeaderText="TaskName" SortExpression="TaskName"></asp:BoundField>
<asp:TemplateField HeaderText="RunTime">
<ItemTemplate>
<asp:TextBox ID="UTCRT" runat="server" Enabled="false" BorderStyle="Dotted" BorderWidth="1px" ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="UTCLRT" runat="server" Enabled="false" BorderStyle="Dotted" BorderWidth="1px" ReadOnly="True"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black"></FooterStyle>

<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White"></HeaderStyle>

<PagerStyle HorizontalAlign="Right" BackColor="White" ForeColor="Black"></PagerStyle>

<SelectedRowStyle BackColor="#6699FF" Font-Bold="True" ForeColor="White"></SelectedRowStyle>

<SortedAscendingCellStyle BackColor="#F7F7F7"></SortedAscendingCellStyle>

<SortedAscendingHeaderStyle BackColor="#4B4B4B"></SortedAscendingHeaderStyle>

<SortedDescendingCellStyle BackColor="#E5E5E5"></SortedDescendingCellStyle>

<SortedDescendingHeaderStyle BackColor="#242121"></SortedDescendingHeaderStyle>
</asp:GridView>


<asp:DetailsView ID="eventDetailsView" runat="server" AutoGenerateRows="False">
<RowStyle HorizontalAlign="Center" />
</asp:DetailsView>


RowDataBound:



foreach (GridViewRow row in tasksGridView.Rows)
{
currentDataIndex = e.Row.RowIndex;
if (row.RowType == DataControlRowType.DataRow)
{
if (utcRnTime != null)
{
try
{
utcLRTUpdate = Tquery[currentDataIndex].Schedules.FirstOrDefault().UtcLastRunTime.Value;
utcRTUpdate = Tquery[currentDataIndex].Schedules.FirstOrDefault().UtcRunTime.Value;


Any help would be appreciated!
I don't think any more code is required.










share|improve this question




















  • 3





    However you should provide where you get that error.

    – HimBromBeere
    Nov 22 '18 at 15:37











  • Have you checked if your methods has the correct items in it? e.g, when browsing though the cells if the count is greater than the number you are querying?

    – mahlatse
    Nov 22 '18 at 15:39











  • You do not check if IntEid = Convert.ToInt32(Eid); is not out of range

    – krzyski
    Nov 22 '18 at 15:43













  • No I havent checked if IntEid is out of range however I know it isnt, for example If I click on row 7 the table has more rows then 7.

    – Boywithanafro
    Nov 22 '18 at 15:45











  • I feel as though it has something to do with .FirstOrDefault()

    – Boywithanafro
    Nov 22 '18 at 15:45














0












0








0








I get an 'index out of range error' however do not no why,



I have gridview master control and am using SelectedIndexChanged to display more information on the selected row in a details view, however when I try to point to the selected row in the query it gives an index out of range error.



I am using a ADO.NET Entity model and want to essentially retrieve data about the selected gridview row using another database table called EventLog and populate the details view with that.




Error Message (Line 50)



Line 50: description =
Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();



Line 51: resultCode =
Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().ResultCode);




Here is my SelectedIndexChanged code:



<!-- language: lang-c# -->
string description;
int resultCode;
int TaskInstanceID;
string Eid;
int IntEid;

//added 's' at end of eventDetailsView due to conflicting naming conventions
DetailsView eventDetailsViews = new DetailsView();

public void tasksGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
eventDetailsView.Visible = true;
//Get's the currently selected row
GridViewRow row = tasksGridView.SelectedRow;
selectedId = row.Cells[1].Text;
System.Diagnostics.Debug.WriteLine(selectedId);

Eid = tasksGridView.SelectedRow.Cells[1].Text;
IntEid = Convert.ToInt32(Eid);

description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();
*///description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc;*
resultCode = Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().ResultCode);
TaskInstanceID = Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().TaskInstanceID);

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Eid", typeof(int)),
new DataColumn("Description", typeof(string)),
new DataColumn("TaskInstanceID", typeof(int))
});
dt.Rows.Add(Eid, description, TaskInstanceID);

eventDetailsViews.DataSource = dt;
eventDetailsView.DataBind();

}


Here is my query code:



    protected void Page_Load(object sender, EventArgs e)
{
infordevEntitiesOrbis dbContext = new infordevEntitiesOrbis();
//Acting as a using SqlConnection to the database
using (infordevEntitiesOrbis context = new infordevEntitiesOrbis())
{
//On pageload, not on user interaction (Occurs regardless)
if (!IsPostBack)
{
d = d.AddMonths(-3);

//Query written using LINQ
Tquery = (from tasks in context.Tasks.AsEnumerable()
select new Task()
{
TaskID = tasks.TaskID,
TaskName = tasks.TaskName,
TaskPath = tasks.TaskPath,
Schedules = tasks.Schedules
}).ToList();


ASP Code for gridview and details view:



 <asp:GridView ID="tasksGridView" runat="server" DataKeyNames="TaskID" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" AutoGenerateColumns="False" AutoGenerateSelectButton="True" OnSelectedIndexChanged="tasksGridView_SelectedIndexChanged" OnSelectedIndexChanging="tasksGridView_SelectedIndexChanging" OnRowDataBound="tasksGridView_RowDataBound1" HorizontalAlign="Center">
<Columns>
<asp:BoundField DataField="TaskID" HeaderText="TaskID" ReadOnly="True" SortExpression="TaskID"></asp:BoundField>
<asp:BoundField DataField="TaskPath" HeaderText="TaskPath" SortExpression="TaskPath"></asp:BoundField>
<asp:BoundField DataField="TaskName" HeaderText="TaskName" SortExpression="TaskName"></asp:BoundField>
<asp:TemplateField HeaderText="RunTime">
<ItemTemplate>
<asp:TextBox ID="UTCRT" runat="server" Enabled="false" BorderStyle="Dotted" BorderWidth="1px" ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="UTCLRT" runat="server" Enabled="false" BorderStyle="Dotted" BorderWidth="1px" ReadOnly="True"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black"></FooterStyle>

<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White"></HeaderStyle>

<PagerStyle HorizontalAlign="Right" BackColor="White" ForeColor="Black"></PagerStyle>

<SelectedRowStyle BackColor="#6699FF" Font-Bold="True" ForeColor="White"></SelectedRowStyle>

<SortedAscendingCellStyle BackColor="#F7F7F7"></SortedAscendingCellStyle>

<SortedAscendingHeaderStyle BackColor="#4B4B4B"></SortedAscendingHeaderStyle>

<SortedDescendingCellStyle BackColor="#E5E5E5"></SortedDescendingCellStyle>

<SortedDescendingHeaderStyle BackColor="#242121"></SortedDescendingHeaderStyle>
</asp:GridView>


<asp:DetailsView ID="eventDetailsView" runat="server" AutoGenerateRows="False">
<RowStyle HorizontalAlign="Center" />
</asp:DetailsView>


RowDataBound:



foreach (GridViewRow row in tasksGridView.Rows)
{
currentDataIndex = e.Row.RowIndex;
if (row.RowType == DataControlRowType.DataRow)
{
if (utcRnTime != null)
{
try
{
utcLRTUpdate = Tquery[currentDataIndex].Schedules.FirstOrDefault().UtcLastRunTime.Value;
utcRTUpdate = Tquery[currentDataIndex].Schedules.FirstOrDefault().UtcRunTime.Value;


Any help would be appreciated!
I don't think any more code is required.










share|improve this question
















I get an 'index out of range error' however do not no why,



I have gridview master control and am using SelectedIndexChanged to display more information on the selected row in a details view, however when I try to point to the selected row in the query it gives an index out of range error.



I am using a ADO.NET Entity model and want to essentially retrieve data about the selected gridview row using another database table called EventLog and populate the details view with that.




Error Message (Line 50)



Line 50: description =
Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();



Line 51: resultCode =
Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().ResultCode);




Here is my SelectedIndexChanged code:



<!-- language: lang-c# -->
string description;
int resultCode;
int TaskInstanceID;
string Eid;
int IntEid;

//added 's' at end of eventDetailsView due to conflicting naming conventions
DetailsView eventDetailsViews = new DetailsView();

public void tasksGridView_SelectedIndexChanged(Object sender, EventArgs e)
{
eventDetailsView.Visible = true;
//Get's the currently selected row
GridViewRow row = tasksGridView.SelectedRow;
selectedId = row.Cells[1].Text;
System.Diagnostics.Debug.WriteLine(selectedId);

Eid = tasksGridView.SelectedRow.Cells[1].Text;
IntEid = Convert.ToInt32(Eid);

description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();
*///description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc;*
resultCode = Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().ResultCode);
TaskInstanceID = Convert.ToInt32(Tquery[IntEid].EventLogs.FirstOrDefault().TaskInstanceID);

DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Eid", typeof(int)),
new DataColumn("Description", typeof(string)),
new DataColumn("TaskInstanceID", typeof(int))
});
dt.Rows.Add(Eid, description, TaskInstanceID);

eventDetailsViews.DataSource = dt;
eventDetailsView.DataBind();

}


Here is my query code:



    protected void Page_Load(object sender, EventArgs e)
{
infordevEntitiesOrbis dbContext = new infordevEntitiesOrbis();
//Acting as a using SqlConnection to the database
using (infordevEntitiesOrbis context = new infordevEntitiesOrbis())
{
//On pageload, not on user interaction (Occurs regardless)
if (!IsPostBack)
{
d = d.AddMonths(-3);

//Query written using LINQ
Tquery = (from tasks in context.Tasks.AsEnumerable()
select new Task()
{
TaskID = tasks.TaskID,
TaskName = tasks.TaskName,
TaskPath = tasks.TaskPath,
Schedules = tasks.Schedules
}).ToList();


ASP Code for gridview and details view:



 <asp:GridView ID="tasksGridView" runat="server" DataKeyNames="TaskID" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" AutoGenerateColumns="False" AutoGenerateSelectButton="True" OnSelectedIndexChanged="tasksGridView_SelectedIndexChanged" OnSelectedIndexChanging="tasksGridView_SelectedIndexChanging" OnRowDataBound="tasksGridView_RowDataBound1" HorizontalAlign="Center">
<Columns>
<asp:BoundField DataField="TaskID" HeaderText="TaskID" ReadOnly="True" SortExpression="TaskID"></asp:BoundField>
<asp:BoundField DataField="TaskPath" HeaderText="TaskPath" SortExpression="TaskPath"></asp:BoundField>
<asp:BoundField DataField="TaskName" HeaderText="TaskName" SortExpression="TaskName"></asp:BoundField>
<asp:TemplateField HeaderText="RunTime">
<ItemTemplate>
<asp:TextBox ID="UTCRT" runat="server" Enabled="false" BorderStyle="Dotted" BorderWidth="1px" ReadOnly="True"></asp:TextBox>
<asp:TextBox ID="UTCLRT" runat="server" Enabled="false" BorderStyle="Dotted" BorderWidth="1px" ReadOnly="True"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Wrap="True" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black"></FooterStyle>

<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White"></HeaderStyle>

<PagerStyle HorizontalAlign="Right" BackColor="White" ForeColor="Black"></PagerStyle>

<SelectedRowStyle BackColor="#6699FF" Font-Bold="True" ForeColor="White"></SelectedRowStyle>

<SortedAscendingCellStyle BackColor="#F7F7F7"></SortedAscendingCellStyle>

<SortedAscendingHeaderStyle BackColor="#4B4B4B"></SortedAscendingHeaderStyle>

<SortedDescendingCellStyle BackColor="#E5E5E5"></SortedDescendingCellStyle>

<SortedDescendingHeaderStyle BackColor="#242121"></SortedDescendingHeaderStyle>
</asp:GridView>


<asp:DetailsView ID="eventDetailsView" runat="server" AutoGenerateRows="False">
<RowStyle HorizontalAlign="Center" />
</asp:DetailsView>


RowDataBound:



foreach (GridViewRow row in tasksGridView.Rows)
{
currentDataIndex = e.Row.RowIndex;
if (row.RowType == DataControlRowType.DataRow)
{
if (utcRnTime != null)
{
try
{
utcLRTUpdate = Tquery[currentDataIndex].Schedules.FirstOrDefault().UtcLastRunTime.Value;
utcRTUpdate = Tquery[currentDataIndex].Schedules.FirstOrDefault().UtcRunTime.Value;


Any help would be appreciated!
I don't think any more code is required.







c# asp.net linq gridview detailsview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 8 at 14:58







Boywithanafro

















asked Nov 22 '18 at 15:36









BoywithanafroBoywithanafro

49111




49111








  • 3





    However you should provide where you get that error.

    – HimBromBeere
    Nov 22 '18 at 15:37











  • Have you checked if your methods has the correct items in it? e.g, when browsing though the cells if the count is greater than the number you are querying?

    – mahlatse
    Nov 22 '18 at 15:39











  • You do not check if IntEid = Convert.ToInt32(Eid); is not out of range

    – krzyski
    Nov 22 '18 at 15:43













  • No I havent checked if IntEid is out of range however I know it isnt, for example If I click on row 7 the table has more rows then 7.

    – Boywithanafro
    Nov 22 '18 at 15:45











  • I feel as though it has something to do with .FirstOrDefault()

    – Boywithanafro
    Nov 22 '18 at 15:45














  • 3





    However you should provide where you get that error.

    – HimBromBeere
    Nov 22 '18 at 15:37











  • Have you checked if your methods has the correct items in it? e.g, when browsing though the cells if the count is greater than the number you are querying?

    – mahlatse
    Nov 22 '18 at 15:39











  • You do not check if IntEid = Convert.ToInt32(Eid); is not out of range

    – krzyski
    Nov 22 '18 at 15:43













  • No I havent checked if IntEid is out of range however I know it isnt, for example If I click on row 7 the table has more rows then 7.

    – Boywithanafro
    Nov 22 '18 at 15:45











  • I feel as though it has something to do with .FirstOrDefault()

    – Boywithanafro
    Nov 22 '18 at 15:45








3




3





However you should provide where you get that error.

– HimBromBeere
Nov 22 '18 at 15:37





However you should provide where you get that error.

– HimBromBeere
Nov 22 '18 at 15:37













Have you checked if your methods has the correct items in it? e.g, when browsing though the cells if the count is greater than the number you are querying?

– mahlatse
Nov 22 '18 at 15:39





Have you checked if your methods has the correct items in it? e.g, when browsing though the cells if the count is greater than the number you are querying?

– mahlatse
Nov 22 '18 at 15:39













You do not check if IntEid = Convert.ToInt32(Eid); is not out of range

– krzyski
Nov 22 '18 at 15:43







You do not check if IntEid = Convert.ToInt32(Eid); is not out of range

– krzyski
Nov 22 '18 at 15:43















No I havent checked if IntEid is out of range however I know it isnt, for example If I click on row 7 the table has more rows then 7.

– Boywithanafro
Nov 22 '18 at 15:45





No I havent checked if IntEid is out of range however I know it isnt, for example If I click on row 7 the table has more rows then 7.

– Boywithanafro
Nov 22 '18 at 15:45













I feel as though it has something to do with .FirstOrDefault()

– Boywithanafro
Nov 22 '18 at 15:45





I feel as though it has something to do with .FirstOrDefault()

– Boywithanafro
Nov 22 '18 at 15:45












3 Answers
3






active

oldest

votes


















0














It seems as you're trying to access an object in the TQuery list on index Eid which might not exist (if the id is say 13020 it will look at that position in the list). Use Tquery.First(t => t.TaskID === Eid).EventLogs... to get the task you're looking for.






share|improve this answer


























  • That is still giving me the error after: description = Tquery[IntEid].EventLogs.First(t => t.TaskID == IntEid).EventDesc;

    – Boywithanafro
    Nov 22 '18 at 16:44













  • Ah, I was a bit unclear. First() should be run Tquery. I have updated my answer.

    – JLe
    Nov 22 '18 at 20:07



















0














Looks like you are trying to look up a task in the list via its ID (I assume Eid and IntEid are the same as Task.TaskID, but it's hard to tell from your code). You can't do that with a list and the property... that goes by index, not ID.



Perhaps you should use a dictionary instead:



Tquery = (
from tasks in context.Tasks.AsEnumerable()
select new Task()
{
TaskID = tasks.TaskID,
TaskName = tasks.TaskName,
TaskPath = tasks.TaskPath,
Schedules = tasks.Schedules
}
)
.ToDictionary
(
task => task.TaskID //Create a dictionary using the TaskID as the key
);


Then when you do this:



description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();


...you will actually be looking up by ID instead of index.






share|improve this answer


























  • I am now unable to .dataBind() the gridview, with error message ' does not contain a property with the name 'TaskID'. Why is that?

    – Boywithanafro
    Nov 23 '18 at 13:09













  • IntEid and Eid are the selected row of the gridview control, storing the value as string and int. They are not the Task.TaskID directly however I use these variables to compare the selected row to the taskID. Still having the same problem above when using a dictionary, may have to re-write a few things to allow a dictionary to work in place of the list

    – Boywithanafro
    Nov 26 '18 at 9:21



















0














I solved this by creating a separate query for the EventLogs table and using the query results to allocate variables 'description', 'resultCode' and 'TaskInstanceID'.



As you can see the new query is almost identical to the old one:



Equery = (from eventLog in context.EventLogs.AsEnumerable()
where eventLog.TaskID == IntEid && eventLog.ResultCode != 0 && eventLog.EventType == 2
select new EventLog()
{
TaskID = eventLog.TaskID,
EventDesc = eventLog.EventDesc,
ResultCode = eventLog.ResultCode,
TaskInstanceID = eventLog.TaskInstanceID
}).ToList();


Then setting the query results here:



        description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();
resultCode = Equery.FirstOrDefault(t => t.TaskID == IntEid).ResultCode.ToString();
TaskInstanceID = Equery.FirstOrDefault(t => t.TaskID == IntEid).TaskInstanceID.ToString();





share|improve this answer
























  • However just using Equery to bind to gridview instead of looping and setting variables description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();

    – Boywithanafro
    Jan 8 at 14:51













  • Alothough I am still unsure why it did not work using Tquery, if anyone knows the particular reason for this please feel free to add an answer!

    – Boywithanafro
    Jan 8 at 15:32











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434244%2findex-out-of-range-cant-get-selectedindexchanged-as-index-for-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














It seems as you're trying to access an object in the TQuery list on index Eid which might not exist (if the id is say 13020 it will look at that position in the list). Use Tquery.First(t => t.TaskID === Eid).EventLogs... to get the task you're looking for.






share|improve this answer


























  • That is still giving me the error after: description = Tquery[IntEid].EventLogs.First(t => t.TaskID == IntEid).EventDesc;

    – Boywithanafro
    Nov 22 '18 at 16:44













  • Ah, I was a bit unclear. First() should be run Tquery. I have updated my answer.

    – JLe
    Nov 22 '18 at 20:07
















0














It seems as you're trying to access an object in the TQuery list on index Eid which might not exist (if the id is say 13020 it will look at that position in the list). Use Tquery.First(t => t.TaskID === Eid).EventLogs... to get the task you're looking for.






share|improve this answer


























  • That is still giving me the error after: description = Tquery[IntEid].EventLogs.First(t => t.TaskID == IntEid).EventDesc;

    – Boywithanafro
    Nov 22 '18 at 16:44













  • Ah, I was a bit unclear. First() should be run Tquery. I have updated my answer.

    – JLe
    Nov 22 '18 at 20:07














0












0








0







It seems as you're trying to access an object in the TQuery list on index Eid which might not exist (if the id is say 13020 it will look at that position in the list). Use Tquery.First(t => t.TaskID === Eid).EventLogs... to get the task you're looking for.






share|improve this answer















It seems as you're trying to access an object in the TQuery list on index Eid which might not exist (if the id is say 13020 it will look at that position in the list). Use Tquery.First(t => t.TaskID === Eid).EventLogs... to get the task you're looking for.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 20:06

























answered Nov 22 '18 at 15:43









JLeJLe

2,51331328




2,51331328













  • That is still giving me the error after: description = Tquery[IntEid].EventLogs.First(t => t.TaskID == IntEid).EventDesc;

    – Boywithanafro
    Nov 22 '18 at 16:44













  • Ah, I was a bit unclear. First() should be run Tquery. I have updated my answer.

    – JLe
    Nov 22 '18 at 20:07



















  • That is still giving me the error after: description = Tquery[IntEid].EventLogs.First(t => t.TaskID == IntEid).EventDesc;

    – Boywithanafro
    Nov 22 '18 at 16:44













  • Ah, I was a bit unclear. First() should be run Tquery. I have updated my answer.

    – JLe
    Nov 22 '18 at 20:07

















That is still giving me the error after: description = Tquery[IntEid].EventLogs.First(t => t.TaskID == IntEid).EventDesc;

– Boywithanafro
Nov 22 '18 at 16:44







That is still giving me the error after: description = Tquery[IntEid].EventLogs.First(t => t.TaskID == IntEid).EventDesc;

– Boywithanafro
Nov 22 '18 at 16:44















Ah, I was a bit unclear. First() should be run Tquery. I have updated my answer.

– JLe
Nov 22 '18 at 20:07





Ah, I was a bit unclear. First() should be run Tquery. I have updated my answer.

– JLe
Nov 22 '18 at 20:07













0














Looks like you are trying to look up a task in the list via its ID (I assume Eid and IntEid are the same as Task.TaskID, but it's hard to tell from your code). You can't do that with a list and the property... that goes by index, not ID.



Perhaps you should use a dictionary instead:



Tquery = (
from tasks in context.Tasks.AsEnumerable()
select new Task()
{
TaskID = tasks.TaskID,
TaskName = tasks.TaskName,
TaskPath = tasks.TaskPath,
Schedules = tasks.Schedules
}
)
.ToDictionary
(
task => task.TaskID //Create a dictionary using the TaskID as the key
);


Then when you do this:



description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();


...you will actually be looking up by ID instead of index.






share|improve this answer


























  • I am now unable to .dataBind() the gridview, with error message ' does not contain a property with the name 'TaskID'. Why is that?

    – Boywithanafro
    Nov 23 '18 at 13:09













  • IntEid and Eid are the selected row of the gridview control, storing the value as string and int. They are not the Task.TaskID directly however I use these variables to compare the selected row to the taskID. Still having the same problem above when using a dictionary, may have to re-write a few things to allow a dictionary to work in place of the list

    – Boywithanafro
    Nov 26 '18 at 9:21
















0














Looks like you are trying to look up a task in the list via its ID (I assume Eid and IntEid are the same as Task.TaskID, but it's hard to tell from your code). You can't do that with a list and the property... that goes by index, not ID.



Perhaps you should use a dictionary instead:



Tquery = (
from tasks in context.Tasks.AsEnumerable()
select new Task()
{
TaskID = tasks.TaskID,
TaskName = tasks.TaskName,
TaskPath = tasks.TaskPath,
Schedules = tasks.Schedules
}
)
.ToDictionary
(
task => task.TaskID //Create a dictionary using the TaskID as the key
);


Then when you do this:



description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();


...you will actually be looking up by ID instead of index.






share|improve this answer


























  • I am now unable to .dataBind() the gridview, with error message ' does not contain a property with the name 'TaskID'. Why is that?

    – Boywithanafro
    Nov 23 '18 at 13:09













  • IntEid and Eid are the selected row of the gridview control, storing the value as string and int. They are not the Task.TaskID directly however I use these variables to compare the selected row to the taskID. Still having the same problem above when using a dictionary, may have to re-write a few things to allow a dictionary to work in place of the list

    – Boywithanafro
    Nov 26 '18 at 9:21














0












0








0







Looks like you are trying to look up a task in the list via its ID (I assume Eid and IntEid are the same as Task.TaskID, but it's hard to tell from your code). You can't do that with a list and the property... that goes by index, not ID.



Perhaps you should use a dictionary instead:



Tquery = (
from tasks in context.Tasks.AsEnumerable()
select new Task()
{
TaskID = tasks.TaskID,
TaskName = tasks.TaskName,
TaskPath = tasks.TaskPath,
Schedules = tasks.Schedules
}
)
.ToDictionary
(
task => task.TaskID //Create a dictionary using the TaskID as the key
);


Then when you do this:



description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();


...you will actually be looking up by ID instead of index.






share|improve this answer















Looks like you are trying to look up a task in the list via its ID (I assume Eid and IntEid are the same as Task.TaskID, but it's hard to tell from your code). You can't do that with a list and the property... that goes by index, not ID.



Perhaps you should use a dictionary instead:



Tquery = (
from tasks in context.Tasks.AsEnumerable()
select new Task()
{
TaskID = tasks.TaskID,
TaskName = tasks.TaskName,
TaskPath = tasks.TaskPath,
Schedules = tasks.Schedules
}
)
.ToDictionary
(
task => task.TaskID //Create a dictionary using the TaskID as the key
);


Then when you do this:



description = Tquery[IntEid].EventLogs.FirstOrDefault().EventDesc.ToString();


...you will actually be looking up by ID instead of index.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 22:29

























answered Nov 22 '18 at 22:18









John WuJohn Wu

30.8k42752




30.8k42752













  • I am now unable to .dataBind() the gridview, with error message ' does not contain a property with the name 'TaskID'. Why is that?

    – Boywithanafro
    Nov 23 '18 at 13:09













  • IntEid and Eid are the selected row of the gridview control, storing the value as string and int. They are not the Task.TaskID directly however I use these variables to compare the selected row to the taskID. Still having the same problem above when using a dictionary, may have to re-write a few things to allow a dictionary to work in place of the list

    – Boywithanafro
    Nov 26 '18 at 9:21



















  • I am now unable to .dataBind() the gridview, with error message ' does not contain a property with the name 'TaskID'. Why is that?

    – Boywithanafro
    Nov 23 '18 at 13:09













  • IntEid and Eid are the selected row of the gridview control, storing the value as string and int. They are not the Task.TaskID directly however I use these variables to compare the selected row to the taskID. Still having the same problem above when using a dictionary, may have to re-write a few things to allow a dictionary to work in place of the list

    – Boywithanafro
    Nov 26 '18 at 9:21

















I am now unable to .dataBind() the gridview, with error message ' does not contain a property with the name 'TaskID'. Why is that?

– Boywithanafro
Nov 23 '18 at 13:09







I am now unable to .dataBind() the gridview, with error message ' does not contain a property with the name 'TaskID'. Why is that?

– Boywithanafro
Nov 23 '18 at 13:09















IntEid and Eid are the selected row of the gridview control, storing the value as string and int. They are not the Task.TaskID directly however I use these variables to compare the selected row to the taskID. Still having the same problem above when using a dictionary, may have to re-write a few things to allow a dictionary to work in place of the list

– Boywithanafro
Nov 26 '18 at 9:21





IntEid and Eid are the selected row of the gridview control, storing the value as string and int. They are not the Task.TaskID directly however I use these variables to compare the selected row to the taskID. Still having the same problem above when using a dictionary, may have to re-write a few things to allow a dictionary to work in place of the list

– Boywithanafro
Nov 26 '18 at 9:21











0














I solved this by creating a separate query for the EventLogs table and using the query results to allocate variables 'description', 'resultCode' and 'TaskInstanceID'.



As you can see the new query is almost identical to the old one:



Equery = (from eventLog in context.EventLogs.AsEnumerable()
where eventLog.TaskID == IntEid && eventLog.ResultCode != 0 && eventLog.EventType == 2
select new EventLog()
{
TaskID = eventLog.TaskID,
EventDesc = eventLog.EventDesc,
ResultCode = eventLog.ResultCode,
TaskInstanceID = eventLog.TaskInstanceID
}).ToList();


Then setting the query results here:



        description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();
resultCode = Equery.FirstOrDefault(t => t.TaskID == IntEid).ResultCode.ToString();
TaskInstanceID = Equery.FirstOrDefault(t => t.TaskID == IntEid).TaskInstanceID.ToString();





share|improve this answer
























  • However just using Equery to bind to gridview instead of looping and setting variables description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();

    – Boywithanafro
    Jan 8 at 14:51













  • Alothough I am still unsure why it did not work using Tquery, if anyone knows the particular reason for this please feel free to add an answer!

    – Boywithanafro
    Jan 8 at 15:32
















0














I solved this by creating a separate query for the EventLogs table and using the query results to allocate variables 'description', 'resultCode' and 'TaskInstanceID'.



As you can see the new query is almost identical to the old one:



Equery = (from eventLog in context.EventLogs.AsEnumerable()
where eventLog.TaskID == IntEid && eventLog.ResultCode != 0 && eventLog.EventType == 2
select new EventLog()
{
TaskID = eventLog.TaskID,
EventDesc = eventLog.EventDesc,
ResultCode = eventLog.ResultCode,
TaskInstanceID = eventLog.TaskInstanceID
}).ToList();


Then setting the query results here:



        description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();
resultCode = Equery.FirstOrDefault(t => t.TaskID == IntEid).ResultCode.ToString();
TaskInstanceID = Equery.FirstOrDefault(t => t.TaskID == IntEid).TaskInstanceID.ToString();





share|improve this answer
























  • However just using Equery to bind to gridview instead of looping and setting variables description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();

    – Boywithanafro
    Jan 8 at 14:51













  • Alothough I am still unsure why it did not work using Tquery, if anyone knows the particular reason for this please feel free to add an answer!

    – Boywithanafro
    Jan 8 at 15:32














0












0








0







I solved this by creating a separate query for the EventLogs table and using the query results to allocate variables 'description', 'resultCode' and 'TaskInstanceID'.



As you can see the new query is almost identical to the old one:



Equery = (from eventLog in context.EventLogs.AsEnumerable()
where eventLog.TaskID == IntEid && eventLog.ResultCode != 0 && eventLog.EventType == 2
select new EventLog()
{
TaskID = eventLog.TaskID,
EventDesc = eventLog.EventDesc,
ResultCode = eventLog.ResultCode,
TaskInstanceID = eventLog.TaskInstanceID
}).ToList();


Then setting the query results here:



        description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();
resultCode = Equery.FirstOrDefault(t => t.TaskID == IntEid).ResultCode.ToString();
TaskInstanceID = Equery.FirstOrDefault(t => t.TaskID == IntEid).TaskInstanceID.ToString();





share|improve this answer













I solved this by creating a separate query for the EventLogs table and using the query results to allocate variables 'description', 'resultCode' and 'TaskInstanceID'.



As you can see the new query is almost identical to the old one:



Equery = (from eventLog in context.EventLogs.AsEnumerable()
where eventLog.TaskID == IntEid && eventLog.ResultCode != 0 && eventLog.EventType == 2
select new EventLog()
{
TaskID = eventLog.TaskID,
EventDesc = eventLog.EventDesc,
ResultCode = eventLog.ResultCode,
TaskInstanceID = eventLog.TaskInstanceID
}).ToList();


Then setting the query results here:



        description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();
resultCode = Equery.FirstOrDefault(t => t.TaskID == IntEid).ResultCode.ToString();
TaskInstanceID = Equery.FirstOrDefault(t => t.TaskID == IntEid).TaskInstanceID.ToString();






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 8 at 14:25









BoywithanafroBoywithanafro

49111




49111













  • However just using Equery to bind to gridview instead of looping and setting variables description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();

    – Boywithanafro
    Jan 8 at 14:51













  • Alothough I am still unsure why it did not work using Tquery, if anyone knows the particular reason for this please feel free to add an answer!

    – Boywithanafro
    Jan 8 at 15:32



















  • However just using Equery to bind to gridview instead of looping and setting variables description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();

    – Boywithanafro
    Jan 8 at 14:51













  • Alothough I am still unsure why it did not work using Tquery, if anyone knows the particular reason for this please feel free to add an answer!

    – Boywithanafro
    Jan 8 at 15:32

















However just using Equery to bind to gridview instead of looping and setting variables description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();

– Boywithanafro
Jan 8 at 14:51







However just using Equery to bind to gridview instead of looping and setting variables description = Equery.FirstOrDefault(t => t.TaskID == IntEid).EventDesc.ToString();

– Boywithanafro
Jan 8 at 14:51















Alothough I am still unsure why it did not work using Tquery, if anyone knows the particular reason for this please feel free to add an answer!

– Boywithanafro
Jan 8 at 15:32





Alothough I am still unsure why it did not work using Tquery, if anyone knows the particular reason for this please feel free to add an answer!

– Boywithanafro
Jan 8 at 15:32


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434244%2findex-out-of-range-cant-get-selectedindexchanged-as-index-for-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?