Answered by:
Specified argument was out of the range of valid values. Parameter name: index

Question
-
User-367318540 posted
when i am giving date parameter then this error is coming
Specified argument was out of the range of valid values.
Parameter name: indexon this line
GVBigbaldailyrpt.Controls[0].Controls.Add(row);
Friday, May 24, 2019 7:46 AM
Answers
-
User61956409 posted
Hi akhterr,
I suggest that you can set break point in your code and debug the project step by step to troubleshoot the issue.
GVBigbaldailyrpt.Controls[1].Controls.Add(row);In your AddTotalRow method, we can find that you use 1 as index to access control from Control Collection, if there is less than 2 controls returned, the code snippet will cause the issue.
With Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 28, 2019 8:10 AM
All replies
-
User61956409 posted
Hi akhterr,
akhterr
Specified argument was out of the range of valid values.
Parameter name: indexThe code snippet GVBigbaldailyrpt.Controls will return a Control Collection, if there are no any control in returned Control Collection and you use GVBigbaldailyrpt.Controls[0] to get the first control, which will cause the issue.
Please make sure GVBigbaldailyrpt.Controls contain controls within it before you access control using index from the returned Control Collection.
With Regards,
Fei Han
Friday, May 24, 2019 9:18 AM -
User-367318540 posted
Dear Fei Han
here is my complete code of c#
protected void btnpbiw_Click(object sender, EventArgs e)
{
// if (!IsPostBack)
{// BindGrid();
}
// private void BindGrid()con.Open();
SqlCommand cmd = new SqlCommand("Spbigbaldailyreport", con);
cmd.Parameters.Add(new SqlParameter("@StartDate", DateTime.Parse(txtitempbstart.Text).ToString("M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture)));
cmd.Parameters.Add(new SqlParameter("@EndDate", DateTime.Parse(txtitempbend.Text).ToString("M/d/yyyy ", System.Globalization.CultureInfo.InvariantCulture)));cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);DataTable dt = new DataTable();
da.Fill(dt);
GVBigbaldailyrpt.DataSource = dt;
GVBigbaldailyrpt.DataBind();
}
int currentid = 0;
decimal subTotal = 0;
decimal total = 0;
int subTotalRowIndex = 0;
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
subTotal = 0;
DataTable dt = new DataTable();
if (e.Row.RowType == DataControlRowType.DataRow)
{
//check the data item is type of dataRowView and table is not null
if (e.Row.DataItem is DataRowView
&& (e.Row.DataItem as DataRowView).DataView.Table != null)
{
//This line throws the error
dt = (e.Row.DataItem as DataRowView).DataView.Table;
int orderId = Convert.ToInt32(dt.Rows[e.Row.RowIndex]["CID"]);
total += Convert.ToInt32(dt.Rows[e.Row.RowIndex]["QTY"]);
if (orderId != currentid)
{
if (e.Row.RowIndex > 0)
{
for (int i = subTotalRowIndex; i < e.Row.RowIndex; i++)
{
subTotal += Convert.ToDecimal(GVBigbaldailyrpt.Rows[i].Cells[4].Text);
}
this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
subTotalRowIndex = e.Row.RowIndex;
}
currentid = orderId;
}
}}
}private void AddTotalRow(string labelText, string value)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal);
row.BackColor = ColorTranslator.FromHtml("#F9F9F9");
row.Cells.AddRange(new TableCell[4] { new TableCell (), new TableCell (),//Empty Cell
new TableCell { Text = labelText, HorizontalAlign = HorizontalAlign.Right},
new TableCell { Text = value, HorizontalAlign = HorizontalAlign.Right } });
GVBigbaldailyrpt.Controls[1].Controls.Add(row);
}protected void OnDataBound(object sender, EventArgs e)
{
for (int i = subTotalRowIndex; i < GVBigbaldailyrpt.Rows.Count; i++)
{
subTotal += Convert.ToDecimal(GVBigbaldailyrpt.Rows[i].Cells[4].Text);
}
this.AddTotalRow("Sub Total", subTotal.ToString("N2"));
this.AddTotalRow("Total", total.ToString("N2"));
con.Close();
}protected void Exporttoexcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GVBigbaldailyrpt.AllowPaging = false;
//BindGrid();
GVBigbaldailyrpt.HeaderRow.Style.Add("background-color", "#FFFFFF");
//GVBigbaldailyrpt.HeaderRow.Cells[0].Style.Add("background-color", "green");
GVBigbaldailyrpt.HeaderRow.Cells[1].Style.Add("background-color", "green");
GVBigbaldailyrpt.HeaderRow.Cells[2].Style.Add("background-color", "green");
GVBigbaldailyrpt.HeaderRow.Cells[3].Style.Add("background-color", "green");
GVBigbaldailyrpt.HeaderRow.Cells[4].Style.Add("background-color", "green");
GVBigbaldailyrpt.HeaderRow.Cells[5].Style.Add("background-color", "green");for (int i = 0; i < GVBigbaldailyrpt.Rows.Count; i++)
{
GridViewRow row = GVBigbaldailyrpt.Rows[i];
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add("class", "textmode");
if (i % 2 != 0)
{
//row.Cells[0].Style.Add("background-color", "#C2D69B");
row.Cells[1].Style.Add("background-color", "#C2D69B");
row.Cells[2].Style.Add("background-color", "#C2D69B");
row.Cells[3].Style.Add("background-color", "#C2D69B");
row.Cells[4].Style.Add("background-color", "#C2D69B");
row.Cells[5].Style.Add("background-color", "#C2D69B");
}
}
GVBigbaldailyrpt.RenderControl(hw);
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
// public override void verifyRenderingInServerForm(Control control) { }
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
}
}this one is HTML.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Bigbaldailyrepot.aspx.cs" Inherits="Saleorder.Bigbaldailyrepot" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtitempbstart" runat="server" TextMode="Date"></asp:TextBox>
To:
<asp:TextBox ID="txtitempbend" runat="server" TextMode="Date"></asp:TextBox>
<asp:Button ID="btnalbb" runat="server" Text="Search" OnClick="btnpbiw_Click" /><asp:LinkButton ID="Exporttoexcel" runat="server" OnClick="Exporttoexcel_Click">Export</asp:LinkButton>
<div>
<asp:GridView ID = "GVBigbaldailyrpt" runat = "server" AutoGenerateColumns = "False" OnDataBound = "OnDataBound" OnRowCreated = "OnRowCreated" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:BoundField DataField = "Artical" HeaderText = "Artical" ItemStyle-Width = "60" >
<ItemStyle Width="60px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField = "Section" HeaderText = "Section" ItemStyle-Width = "60" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField = "Catagory" HeaderText = "Catagory" ItemStyle-Width = "60" >
<ItemStyle Width="100px"></ItemStyle>
</asp:BoundField><asp:BoundField DataField = "CID" HeaderText = "CID" ItemStyle-Width = "0" Visible="false" >
<ItemStyle Width="150px"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField = "QTY" HeaderText = "QTY" ItemStyle-Width = "60" DataFormatString="{0:N2}"
ItemStyle-HorizontalAlign="Right" >
<ItemStyle HorizontalAlign="Right" Width="60px"></ItemStyle>
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
</div>
</form>
</body>
</html>please have a look on this...thanks
Friday, May 24, 2019 9:51 AM -
User61956409 posted
Hi akhterr,
I suggest that you can set break point in your code and debug the project step by step to troubleshoot the issue.
GVBigbaldailyrpt.Controls[1].Controls.Add(row);In your AddTotalRow method, we can find that you use 1 as index to access control from Control Collection, if there is less than 2 controls returned, the code snippet will cause the issue.
With Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 28, 2019 8:10 AM