logo
down
shadow

Asp.net System.ArgumentOutOfRangeException Error with ListBox SelectedIndex


Asp.net System.ArgumentOutOfRangeException Error with ListBox SelectedIndex

By : user2951478
Date : November 18 2020, 11:13 AM
this one helps. The problem here is that you have two ListBoxes, one (tweetid) storing the id's (presumably a key of sorts) of the tweets, and the other (ListBox2) storing the text of the tweets. You also don't seem to be adding the dynamically created tweetid ListBox to the page anywhere (and presumably the intention is that the tweetid ListBox is hidden or similar).
You are then attempting to store synchronize the selected text drop down back to the original tweetid in the code behind Button click handler, based on the relative indexes of the data - it is apparant that the two listboxes are getting out of synch, plus, you aren't validating the user has selected a valid item, hence the ArgumentOutOfRange exception.
code :
<asp:ListBox runat="server" ID="TweetsListBox" DataTextField="Text" DataValueField="Id" EnableViewState="True" />
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // Replace with your code to fetch tweets here
        TweetsListBox.DataSource = FetchSomeTweets();
        // We've alread set the names of the properties to use `Id` and `Text` in the aspx
        TweetsListBox.DataBind();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    long tweetIdToRetweet;
    if (long.TryParse(TweetsListBox.SelectedValue, out tweetIdToRetweet))
    {
        servis.Retweet(new RetweetOptions() { Id = tweetIdToRetweet });
    }
    else
    {
        // Display Error that user must select a tweet
    }
}
class Tweet
{
    public int Id { get; set; }
    public string Text { get; set; }
}

private static readonly IEnumerable<Tweet> SomeTweets = new[]
{
    new Tweet { Id = 123, Text = "This is tweet 123" },
    new Tweet { Id = 234, Text = "This is tweet 234" },
    new Tweet { Id = 345, Text = "This is tweet 345" }
};


Share : facebook icon twitter icon
System.ArgumentOutOfRangeException Error

System.ArgumentOutOfRangeException Error


By : Elizabeth
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You have the exception thrown at Substring(0, 3) or Remove(0, 3) in case either str or clearStr is shorter than 3. I suggest adding validation:
code :
public static string ToCamelCase(string str) {
  // if str is null or too short 
  if (string.IsNullOrEmpty(str))
    return str;
  else if (str.Length < 3)
    return str;

  TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
  string clearStr = Regex.Replace(myTI.ToTitleCase(str), @"_|-", "");

  // if clearStr is too short
  if (clearStr.Length < 3)
    return str;

  return clearStr = str.Substring(0, 3) + clearStr.Remove(0, 3);
}
System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'

System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'SelectedIndex'


By : Joel Fredd Sanchez
Date : March 29 2020, 07:55 AM
around this issue If this way I can list exist video capture devices: , before you add Device.Name, check if it exists in the first place
code :
if(Device!= null){
     comboBox1.Items.Add(Device.Name);
}
Check if listbox.SelectedIndex == listbox.Items.Count from BackgroundWorker

Check if listbox.SelectedIndex == listbox.Items.Count from BackgroundWorker


By : Wiggins
Date : March 29 2020, 07:55 AM
this one helps. This basically happens when you access the forms property from another thread, that's why this exception is thrown. Your UI operations must be performed on the owning thread.
You can do this:
code :
 int intIndex = 0;
 int intCount = 0;
        if (listBox1.InvokeRequired)
        {
            listBox1.Invoke(new MethodInvoker(delegate { intIndex = listBox1.SelectedIndex ; }));
        }

        if (listBox1.InvokeRequired)
        {
            listBox1.Invoke(new MethodInvoker(delegate { intCount = listBox1.Items.Count; }));
        }
        if (intIndex == intCount)
        {
            // TODO: Business Logic
        }
CheckForIllegalCrossThreadCalls = false;
How to debug a System.ArgumentOutOfRangeException error in C#?

How to debug a System.ArgumentOutOfRangeException error in C#?


By : TheMatto
Date : March 29 2020, 07:55 AM
This might help you This code is supposed to take two samples one original and one new, then determine the length of the smallest single consecutive piece that has been inserted into the first sequence. , The problem is:
code :
sample1 = sample1.Substring(i, sample1.Length);
Setting the SelectedIndex of a ComboBox throws ArgumentOutOfRangeException

Setting the SelectedIndex of a ComboBox throws ArgumentOutOfRangeException


By : user3824726
Date : March 29 2020, 07:55 AM
around this issue Aren't I quite the idiot. The selected index changed event was firing, and in that code a different combobox had it's selectedIndex set to 0 - however with certain data it wasn't being populated, and there was no basic error checking before setting the selectedIndex.
Sigh.
Related Posts Related Posts :
  • Can I organize Git submodules in a flat hierarchy?
  • How to make a server reply after receiving a request/data
  • Stored Procedure works in DB but not in C# code
  • MVVM WPF Programmatically add Child Elements
  • NCrunch and PostSharp Logging clash
  • Increasing memory usage in socket client
  • Multiple DropDownList but same action
  • How to get application memory usage as shown in Task Manager?
  • Exposing interfaces instead of concrete classes
  • How to get only postal_code data from google api?
  • Is there a way to get rid of the (by dot42) using community licence?
  • Why must I implement a function for an interface in vb.net which apparently doesn't need to be implemented in C#
  • How to remove HTML tags from the gridview Row
  • Get public post from Facebook's Graph Search API 2.x with Facebook SDK for .NET
  • C# DLL Export to Inno-Setup - Error E0434F4D
  • syntax error insert into database
  • By-pass else block of int.TryParse in Lambda Syntax
  • Retrieve "property getter" from the property name
  • Implementing generic class and multiple interfaces
  • data bind DropDownList with OleDbDataReader from Ms access 2013
  • extract specific data from HTML -CDATA- pattern in C#
  • how to compare password with salted hashed password
  • Excluding one item from list (by Index), and take all others
  • How do I find out which argument is invalid when receiving 'The best overloaded match for (...) has some invalid argumen
  • How do I use a linq query to update the underlying database table
  • How to change the coordinates of a text in a pdf page from lower left to upper left
  • Entity Framework Error: error loading specified metadata resource
  • Writing one character at a time in a C# Console Application?
  • Read write barcode data without using barcode scanner device
  • Asp.net Website is only accessible on our company internet connection and not from other network
  • Generic method to map objects of different types
  • Back button control in windows phone app
  • Get matching enum int values from list of strings
  • Ajax Control Toolkit AutoCompleteExtender displays html source character by character of the current page as autocomplet
  • How to select a node with namespace from XmlDocument?
  • multiple foreach loops inside while loop
  • Why does setting a MinimumSize break the table layout?
  • WatIn SelectList().SelectByValue() return error Run script failed
  • SignalR not works on Google Chrome
  • Sync sent mail with Sent folder
  • return a View and File in a single method in ASP.Net MVC
  • FastReport mvc GetParameter() returns null
  • How to check datatable select method returns datatable c#
  • How to sort an array based on the occurrences of the elements in a string in C#?
  • App crashes without throwing any exception in windows phone 8.1 xaml
  • How to get values from command argument from gridview on link button's click?
  • What's the proper way to convert a Vector3DF into byte array?
  • Is there a way to place a default image for control in edit/view mode?
  • How to set a click event for button in repeater control
  • how to make communication with two different process in same computer?
  • Which is the best crash logging tool for Windows Phone 8 Apps
  • Custom MarkerStyle in MS Chart Control
  • while opening the page getting error there is no row at position 0
  • Error message in Monogame: Unhandled Exception: System.DllNotFoundException: SDL2.dll
  • How to extract "Message" part from Google API error message
  • Receiving Gigs of data from Mobile Service on UpdateAsync method
  • Can't figure out what this is? Unrecognized escape sequence (Mono)
  • Calling stored procedure from another stored procedure with parameters
  • Bind Strings from an .resw file with ReswFileCodeGenerator in XAML
  • wpf DataGrid column index is not saved
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org