1

I try to read all the fields of a column (second column -link-) in my database. I find this example, but I can't manage to read one column of varchar type:

SQLConnection.Open();

using (SqlCommand command = new SqlCommand("SELECT link  FROM shop", SQLConnection))
using (SqlDataReader reader = command.ExecuteReader())
{
    while (reader.Read())
    {
        Console.WriteLine("{2}",reader.GetString(2));
    }
}

I've got this error:

Index was outside the bounds of the array.

and I don't understand why...

Thanks in advance :)

2
  • try SELECT * FROM shop, cause you're getting the second column with reader.GetString(2) Commented Jul 13, 2013 at 22:56
  • I get this error: > Index (zero based) must be greater than or equal to zero and less than the size of the argument list. Commented Jul 13, 2013 at 23:04

1 Answer 1

2

Replace

Console.WriteLine("{2}",reader.GetString(2));

with

Console.WriteLine("{0}",reader.GetString(0));

since 1.)the composite formatting feature uses zero-based indexed placeholders and 2.) you are selecting just one field from the table, so the only index is 0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.