编译器错误消息:CS0021

osh*_*nen 0 c# asp.net asp.net-3.5 .net-3.5

任何人都知道为什么以下不起作用?

C#:

public partial class Default : System.Web.UI.Page
{
    SqlConnection connection;
    SqlCommand command;
    SqlDataReader reader;

    protected void Page_Load(object sender, EventArgs e)
    {
        using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
        {
            using (command = new SqlCommand("select col1, col2 from table1 where id = @id", connection))
            {
                command.Parameters.Add("@id", SqlDbType.Int, 3).Value = 1;
                connection.Open();

                using (reader = command.ExecuteReader())
                {
                    reader.Read();
                    Div1.InnerHtml = reader.Read["col1"].ToString();
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<div runat="server" id="Div1"></div>
Run Code Online (Sandbox Code Playgroud)

错误:

Compiler Error Message: CS0021: Cannot apply indexing with [] to an expression of type 'method group'
Line 32: Div1.InnerHtml = reader.Read["col1"].ToString();
Run Code Online (Sandbox Code Playgroud)

arc*_*hil 9

改变这个

Div1.InnerHtml = reader.Read["col1"].ToString();
Run Code Online (Sandbox Code Playgroud)

进入这个

Div1.InnerHtml = reader["col1"].ToString();
Run Code Online (Sandbox Code Playgroud)