开发者

Index (zero based) must be greater than or equal to zero

开发者 https://www.devze.com 2023-02-17 13:09 出处:网络
Hey I keep getting an error: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

Hey I keep getting an error:

Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

My code:

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}", reade开发者_运维百科r.GetString(0));
}


Your second String.Format uses {2} as a placeholder but you're only passing in one argument, so you should use {0} instead.

Change this:

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

To this:

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


In this line:

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

The token {2} is invalid because you only have one item in the parms. Use this instead:

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


Change this line:

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


This can also happen when trying to throw an ArgumentException where you inadvertently call the ArgumentException constructor overload

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);

}


String.Format must start with zero index "{0}" like this:

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


Change this line:

The 2 should be 0. Every count starts at 0.

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

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


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();

        }
    }
}

Index (zero based) must be greater than or equal to zero

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号