数据为什么会重复添加1个?
把一个textbox中的数据添加到数据库中,可以每次点击按钮后都回添加两个相同的数据。
后台代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace quanliwenCsharp
{
/// <summary>
/// Addtype 的摘要说明。
/// </summary>
public class Addtype : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button BtnAddtype;
protected System.Web.UI.WebControls.TextBox txttype;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.BtnAddtype.Click += new System.EventHandler(this.BtnAddtype_Click);
}
#endregion
private void BtnAddtype_Click(object sender, System.EventArgs e)
{
//添加到数据库
String strConnection = ConfigurationSettings.AppSettings["connectionString"];
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
String strsql;
strsql = "insert producetype (ttype) values (@txttype)";
SqlCommand objCommand = new SqlCommand(strsql,objConnection);
objCommand.Parameters.Add("@txttype", SqlDbType.NVarChar, 50);
objCommand.Parameters["@txttype"].Value = txttype.Text;
SqlDataReader objReader = objCommand.ExecuteReader();
objReader.Close();
objConnection.Close();
}
}
}