1

我是 c#.net 新手。这是我的表格。

First Name: <asp:TextBox ID="f_name" runat="server"></asp:TextBox>
<asp:Button ID="btnInsertion" runat="server" Text="Insert" 
OnClick="btnInsertion_Click"  />

我需要帮助来解决 Visual Basic 中的这两个错误。这是我的 xxxx.aspx.cs,它给了我两个 CS0103 错误 -f_name 在上下文中不存在。-txtFname 在上下文中不存在。

protected void btnInsertion_Click(object sender, EventArgs e)
{

        using (NpgsqlConnection connection = new NpgsqlConnection())
        {

        connection.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();
            connection.Open();
            NpgsqlCommand cmd = new NpgsqlCommand();
            cmd.Connection = connection;
            cmd.CommandText = "Insert into student_folio values(@f_name)";
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.Add(new NpgsqlParameter(@f_name, txtFname.Text));

            cmd.ExecuteNonQuery();
            cmd.Dispose();
        connection.Close();
        txtFname.Text = "";            s

        }

}
4

1 回答 1

1

我想你忘了附上@f_nameParameters.Add(txtFname也不在范围内,它是否存在于你正在访问的页面上btnInsertion

protected void btnInsertion_Click(object sender, EventArgs e)
{

        using (NpgsqlConnection connection = new NpgsqlConnection())
        {

        connection.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString();
            connection.Open();
            NpgsqlCommand cmd = new NpgsqlCommand();
            cmd.Connection = connection;
            cmd.CommandText = "Insert into student_folio (fieldname) values(@f_name)";
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.Add(new NpgsqlParameter("f_name", txtFname.Text));

            cmd.ExecuteNonQuery();
            cmd.Dispose();
        connection.Close();
        txtFname.Text = "";

        }

}
于 2019-08-23T02:27:05.270 回答