using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Add_drugs2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\R.mdf;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into drugs2 (Science_name) values('" + TextBox1.Text + "');
cmd.ExecuteNonQuery();
}
}
这是我的标记文件
<asp:TextBox ID="TextBox1" runat="server" Width="152px"></asp:TextBox>
我收到此错误名称“TextBox1”在当前上下文中不存在 我该如何解决这个错误?我尝试将数据添加到我的数据库
请多思考多尝试。 您可以先获取 TextBox1 的文本,然后将其保存到临时字符串中。 一些代码如下所示:
string text = TextBox1.Text;
cmd.CommandText = "insert into drugs2 (Science_name) values('"+ text + "')";