L'indice (in base zero) deve essere maggiore o uguale a zero


117

Ehi continuo a ricevere un errore:

L'indice (in base zero) deve essere maggiore o uguale a zero e minore della dimensione dell'elenco di argomenti.

Il mio codice:

OdbcCommand cmd = new OdbcCommand("SELECT FirstName, SecondName, Aboutme FROM User WHERE UserID=1", cn);

OdbcDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
    Name.Text = String.Format("{0} {1}", reader.GetString(0), reader.GetString(1));
    Aboutme.Text = String.Format("{2}", reader.GetString(0));
}

6
Hai trasposto l'indice dell'argomento nel lettore con l'indice del parametro nell'istruzione di formato. Cambia 2 con 0 nel tuo Aboutme.Text = .
tvanfosson

9
String.Format non utilizza segnaposto univoci per classe né per soluzione. È per stringa ogni volta che viene chiamato String.Format, quindi non aumentarlo a {2} in base a {0} e {1} che è stato utilizzato!
RichardTheKiwi

1
qualsiasi motivo stai usando ODBC rispetto al connettore .NET?
Jon Black

2
Perché usi string.formar per questa riga :) Aboutme.Text = String.Format ("{2}", reader.GetString (0)); potresti. Aboutme.Text = reader.GetString (0);
Ivo

Risposte:


190

Il tuo secondo String.Formatusa {2}come segnaposto ma stai solo passando un argomento, quindi dovresti usare {0}invece.

Cambia questo:

String.Format("{2}", reader.GetString(0));

A questa:

String.Format("{0}", reader.GetString(2));

23

In questa riga:

Aboutme.Text = String.Format("{2}", reader.GetString(0));

Il token {2} non è valido perché hai solo un elemento nei parametri. Usa questo invece:

Aboutme.Text = String.Format("{0}", reader.GetString(0));

8

Cambia questa riga:

Aboutme.Text = String.Format("{0}", reader.GetString(0));

4

Ciò può accadere anche quando si tenta di lanciare un oggetto in ArgumentExceptioncui si chiama inavvertitamente il ArgumentExceptionsovraccarico del costruttore

public static void Dostuff(Foo bar)
{

   // this works
   throw new ArgumentException(String.Format("Could not find {0}", bar.SomeStringProperty));

   //this gives the error
   throw new ArgumentException(String.Format("Could not find {0}"), bar.SomeStringProperty);

}

2

String.Format deve iniziare con zero indice "{0}" in questo modo:

Aboutme.Text = String.Format("{0}", reader.GetString(0));

1
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Enter Your FirstName ");
            String FirstName = Console.ReadLine();

            Console.WriteLine("Enter Your LastName ");
            String LastName = Console.ReadLine();
            Console.ReadLine();

            Console.WriteLine("Hello {0}, {1} ", FirstName, LastName);
            Console.ReadLine();

        }
    }
}

Immagine


1
Ecco, quando eseguo questa query. Nella riga di comando non stampa l'ultima riga come "Hello Parag Patel" ma mostra l'errore "Si è verificata un'eccezione System.FormatException Messaggio = L'indice (in base zero) deve essere maggiore o uguale a zero e minore della dimensione dell'argomento elenco."
parag

Dovrebbe essere Console.WriteLine ("Hello {0}, {1}", FirstName, LastName);
Fenrir88

@ Fenrir88, risolto
jt000

0

Cambia questa riga:

Il 2 dovrebbe essere 0. Ogni conteggio inizia da 0.

//Aboutme.Text = String.Format("{2}", reader.GetString(0));//wrong

//Aboutme.Text = String.Format("{0}", reader.GetString(0));//correct

0

Nel mio caso non sono riuscito a vedere l'errore "+ nome" . Il compilatore non segnalerebbe un errore in questo caso. Per cui riguardati.

//Wrong Code:

string name="my name";
string age=25;
String.Format(@"Select * from table where name='{1}' and age={1}" +name, age);


//Right Code:

string name="my name";
string age=25;
String.Format(@"Select * from table where name='{1}' and age={1}" , name, age);
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.