<div class="form-group">
<span class="auto-style2">
<label class="col-xs control-label">First Name:</label>
</span>
<input type="text" name="Name" id="Name" value="<%:get_signUp_values()[0] %>" class="form-control" />
</div>
<div class="form-group">
<span class="auto-style2">
<label class="col-xs control-label">Last Name:</label>
</span>
<input type="text" name="lastName" id="lastName" value="<%:get_signUp_values()[1] %>" class="form-control" />
</div>
<div class="form-group">
<span class="auto-style2">
<label class="col-xs control-label">Birthday:</label>
</span>
<input type="text" name="dob" id="dob" value="<%:get_signUp_values()[2] %>-<%:get_signUp_values()[3] %>-<%:get_signUp_values()[4] %>" class="form-control" />
</div>
this is the code which is giving error
value="<%:get_signUp_values()[0] %>"
This is giving an IndexOutOfRangeException: Index was outside the bounds of the array.
. This error is occurring on somee, but not on localhost
.
Code-behind:
public List<HtmlString> get_signUp_values()
{
int userid = Convert.ToInt32(Session["USERID"]);
List<DAO_SignUp> get_signUp_val = new List<DAO_SignUp>(DAO_SignUp.get_signUp(userid));
List<HtmlString> get_values = new List<HtmlString>();
foreach (var obj2 in get_signUp_val)
{
get_values.Add(new HtmlString(obj2.get_First_name));
get_values.Add(new HtmlString(obj2.get_last_name));
string dob = obj2.get_dob.ToString();
string patren = "-";
string[] substrings = Regex.Split(dob, patren);
string day = substrings[0];
string month = substrings[1];
string year = substrings[2];
get_values.Add(new HtmlString(day));
get_values.Add(new HtmlString(month));
get_values.Add(new HtmlString(year));
}
return get_values;
}
IndexOutOfRangeException occurs when you try to access an array index that does not exist.
By looking at your code, there are only two places where the problem could lie.
First, I can see that the collection is filled by looping on the results of that DAO_SignUp call:
List<DAO_SignUp> get_signUp_val = new List<DAO_SignUp>(DAO_SignUp.get_signUp(userid));
foreach (var obj2 in get_signUp_val) (...)
We can then deduce that the problem could be that DAO_SignUp.get_signUp(userid)
is not returning anything on the server, thus the collection returned by the function get_signUp_values()
would have no elements and when trying to access the resulting index of that function, the error is raised.
As you said it is running just fine on localhost, you should then check the configurations on that DAO_SignUp. If it connects to a Database, check if it has data, and if the connection string points to the correct database.
The other option is, as pointed in the comments by L.B, the array returned by the Regex.Split
inside the loop:
string[] substrings = Regex.Split(dob, patren);
string day = substrings[0];
string month = substrings[1];
string year = substrings[2];
If the Regex.Split
returns nothing, that error will be raised. In this case, check for the Length of substrings
before accessing it's index.