我是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
}
}
答案 0 :(得分:1)
我认为您已经忘记将@f_name
括在Parameters.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 = "";
}
}