Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating asp.net web application in visual studio 2017. In aspx page i have give ajaxcontroltoolkit for creating tab container. The tab panel will be created dynamically. This panel are getting from DataSet C# Asp.net and content is binding to gridview all set OK. Below the tab content there is asp.net button this onclick event is fired yesterday when i have created yesterday before creating tab container. Now the problem is when i click asp.net button getting error as mentioned below. Can anyone explain me how to get rid of this error. Thanks in advance

Server Error in '/' Application.
Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index]
   System.Web.UI.ControlCollection.get_Item(Int32 index) +9822693
   AjaxControlToolkit.TabPanelCollection.get_Item(Int32 index) +61
   AjaxControlToolkit.TabContainer.LoadClientState(String clientState) +305
   AjaxControlToolkit.ScriptControlBase.LoadPostData(String postDataKey, NameValueCollection postCollection) +176
   AjaxControlToolkit.TabContainer.LoadPostData(String postDataKey, NameValueCollection postCollection) +105
   AjaxControlToolkit.ScriptControlBase.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +69
   System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +758
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1833


What I have tried:

<div style="margin-bottom: 434px">
    <asp:UpdatePanel ID="upTabContainer" runat="server">
        <ContentTemplate>
            <cc1:TabContainer ID="SubjectCategoryTabs" runat="server"                                  Visible="true">
            </cc1:TabContainer>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
Posted
Updated 4-Dec-18 18:25pm

You are trying to access an array or List and specifying an index value that is either negative or larger than the largest index available. For example, if you have three elements in an array or List, then indexes 0, 1, and 2 are valid, but any other value at all will cause an exception.

Look at your code - use your stack trace to identify your method - and either use the debugger or add logging information to work out exactly which collection and index is involved and the values being tried.

We can't do any of that for you: we don't have access to your site, you code, or your data!
 
Share this answer
 
Comments
Member 8583441 4-Dec-18 5:55am    
I am posting my code please help me. And second thing i need to have the selected value of radio button in stringbuilder how can i achieve this.
void AddTabPanels()
{
SubjectCategoryTabs.Tabs.Clear();
foreach (DataTable tb in ds.Tables)
{
for (int i = 0; i < tb.Rows.Count; i++)
{
string name = tb.Rows[i]["SubjectCategory"].ToString();
AjaxControlToolkit.TabPanel panel = new AjaxControlToolkit.TabPanel
{
HeaderText = name.ToString()
};
string sQuestionsAndOptions = BindQuestionsAndChoices(name);
panel.Controls.Add(new Literal { Text = sQuestionsAndOptions });
SubjectCategoryTabs.Tabs.Add(panel);
}
}
}

private string BindQuestionsAndChoices(string value)
{
StringBuilder htmlTable = new StringBuilder();
SqlConnection con = new SqlConnection(DbBridge.DbConnection);
try
{
if (con.State == ConnectionState.Closed)
con.Open();
string query = "SELECT * FROM QuestionsChoice WHERE Subject = '" + Session["Subject"] + "' AND SubjectCategory = '" + value + "' ORDER BY QuestionNumber ASC";
SqlDataAdapter sda = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
sda.Fill(dt);
htmlTable.Append("");
if (!object.Equals(dt, null))
{
StringBuilder sbQA = new StringBuilder();
sbQA.AppendLine(@"");
sbQA.AppendLine(@"");
sbQA.AppendLine(@"");
sbQA.AppendLine(@"");
sbQA.AppendLine(@"");
sbQA.AppendLine(@"");
sbQA.AppendLine(@"");
sbQA.AppendLine(@"");
sbQA.AppendLine(@"");
string aQuestion = sbQA.ToString();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
htmlTable.AppendFormat(aQuestion, dt.Rows[i]["QuestionNumber"], dt.Rows[i]["Question"], dt.Rows[i]["ChoiceA"], dt.Rows[i]["ChoiceB"], dt.Rows[i]["ChoiceC"], dt.Rows[i]["ChoiceD"]);
}
htmlTable.Append("{0}) {1}<input type=""radio"" id=""rb1{0}"" name=""options{0}"" value=""A""> A) <label for=""rb1{0}"">{2}<input type=""radio"" id=""rb2{0}"" name=""options{0}"" value=""B""> B) <label for=""rb2{0}"">{3}<input type=""radio"" id=""rb3{0}"" name=""options{0}"" value=""C""> C) <label for=""rb3{0}"">{4}<input type=""radio"" id=""rb4{0}"" name=""options{0}"" value=""D""> D) <label for=""rb4{0}"">{5}");
}
else
{
htmlTable.Append(@"There are No Records.");
htmlTable.Append("");
}
}
}
catch(Exception ex)
{
ClientScript.RegisterClientScriptBlock(GetType(), "exception", "alert('" + ex.Message + "');", true);
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
return htmlTable.ToString();
}
OriginalGriff 4-Dec-18 6:04am    
We have no idea - we can't run that code in isolation and don't have any access to the data behind it if we could!
Seriously, this is up to you to gather information - and that means the debugger or logging. Without it, we can do even less than you can!

But please, don't do SQL like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
DROP TABLE MyTable;
A perfectly valid "delete the table" command
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
If you've been doing that through your whole website, you will lose your DB - and the chances are a visitor from a thousand KM away won't even have to log in to do it...
My problem is solved with considering autoeventwireup=false in page directive. Actually by default it is true when i set it to false and this button is placed unside updatepanel with updatemode=condition. Now the button looks good and working like a charm means onclick event is happening. This autoeventwireup=false to set i have referred from
ASP.NET - disable autopostback property for a button server control.[^]

Thanks to prairiedog sir by giving me the solution.
 
Share this answer
 
Comments
Member 8583441 5-Dec-18 0:55am    
If i apply AutoEventWireup="false" button click event is firing but server controls not visible like asp.net label, ajax tabcontainer etc. How to solve this problem. If i apply AutoEventWireup="true" then server controls are visible but button click event is not firing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900