谢谢,我是漏了myCommand.Connection.open(); 这句。现在可以运行了并显示数据,但还是不能进行 insert操作,会出现自己定义的异常。ERROR: Could not add record, please ensure the fields are correctly filled out (不关sa密码的,我改来了)先附上源代码,帮帮看看吧========-----------insert.aspx.csf----------------========== using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Text; 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 Sharp { /// <summary> /// register 的摘要说明。 /// </summary> public class register : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Label Label2; protected System.Web.UI.WebControls.Label Label3; protected System.Web.UI.WebControls.Label Label4; protected System.Web.UI.WebControls.Label Label5; protected System.Web.UI.WebControls.DataGrid DG; protected System.Web.UI.WebControls.TextBox id; protected System.Web.UI.WebControls.TextBox nickname; protected System.Web.UI.WebControls.TextBox name; protected System.Web.UI.WebControls.Button reg; protected System.Web.UI.WebControls.TextBox psw; protected System.Web.UI.WebControls.Label Message; protected System.Web.UI.WebControls.TextBox city;
protected void Page_Load(object sender, System.EventArgs e) { SqlConnection myConnection=new SqlConnection("Server=(local);Database=Sharp;Trusted_Connection=yes"); if(!IsPostBack) BindGrid(); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); }
/// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.reg.Click += new System.EventHandler(this.reg_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion public void reg_Click(object sender, System.EventArgs e) { if(id.Text==""||psw.Text==""||nickname.Text==""||name.Text=="") { Message.Text="You must input the id ,密码,呢称或姓名"; Message.Style["color"]="red"; BindGrid(); return ; } String insertCmd="insert into member values(@Id,"+ "@Nickname ,@Name,@Psw)"; SqlConnection myConnection=new SqlConnection("Server=(local);Database=Sharp;Trusted_Connection=yes"); SqlCommand myCommand =new SqlCommand(insertCmd,myConnection); myCommand.Parameters.Add(new SqlParameter("@Id",SqlDbType.VarChar,10)); myCommand.Parameters["@Id"].Value=id.Text; myCommand.Parameters.Add(new SqlParameter("@Nickname",SqlDbType.VarChar,20)); myCommand.Parameters["@Nickname"].Value=nickname.Text; myCommand.Parameters.Add(new SqlParameter("@Name",SqlDbType.VarChar,80)); myCommand.Parameters["@Name"].Value=name.Text; myCommand.Parameters.Add(new SqlParameter("@Psw",SqlDbType.VarChar,16)); myCommand.Parameters["@Psw"].Value=psw.Text; //myCommand.Parameters.Add(new SqlParameter("@city",SqlDbType.VarChar,50)); //myCommand.Parameters["@City"].Value=city.Text; myCommand.Connection.Open();//important try { myCommand.ExecuteNonQuery(); //myCommand.ExecuteReader(); Message.Text="<b>Add Successfully </b>"+insertCmd; } catch(SqlException ee) { if(ee.Number==2627) Message.Text="<b>ERROR: A record already exists with" + " the same primary key"+insertCmd; else Message.Text = "ERROR: Could not add record, please " + " ensure the fields are correctly filled out"; Message.Style["color"] = "red"; } myCommand.Connection.Close(); BindGrid(); }
public void BindGrid() { SqlConnection myConnection =new SqlConnection("Server=(local);Database=Sharp ;Trusted_Connection=yes"); SqlDataAdapter myDataAdapter=new SqlDataAdapter("Select id ,nickname,name,psw,city from member",myConnection); DataSet DS=new DataSet(); myDataAdapter.Fill(DS); DG.DataSource=DS; DG.DataBind(); } } }
String insertCmd="insert into member values(@Id,"+ "@Nickname ,@Name,@Psw)"; String insertCmd="insert into member(Id,Nickname,Name,Psw) valuse(@Id,"+"@Nickname ,@Name,@Psw)";没有字段怎么添加
这句。现在可以运行了并显示数据,但还是不能进行
insert操作,会出现自己定义的异常。ERROR: Could not add record, please ensure the fields are correctly filled out (不关sa密码的,我改来了)先附上源代码,帮帮看看吧========-----------insert.aspx.csf----------------==========
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;
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 Sharp
{
/// <summary>
/// register 的摘要说明。
/// </summary>
public class register : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.DataGrid DG;
protected System.Web.UI.WebControls.TextBox id;
protected System.Web.UI.WebControls.TextBox nickname;
protected System.Web.UI.WebControls.TextBox name;
protected System.Web.UI.WebControls.Button reg;
protected System.Web.UI.WebControls.TextBox psw;
protected System.Web.UI.WebControls.Label Message;
protected System.Web.UI.WebControls.TextBox city;
protected void Page_Load(object sender, System.EventArgs e)
{
SqlConnection myConnection=new SqlConnection("Server=(local);Database=Sharp;Trusted_Connection=yes");
if(!IsPostBack)
BindGrid();
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.reg.Click += new System.EventHandler(this.reg_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
public void reg_Click(object sender, System.EventArgs e)
{
if(id.Text==""||psw.Text==""||nickname.Text==""||name.Text=="")
{
Message.Text="You must input the id ,密码,呢称或姓名";
Message.Style["color"]="red";
BindGrid();
return ;
}
String insertCmd="insert into member values(@Id,"+
"@Nickname ,@Name,@Psw)";
SqlConnection myConnection=new SqlConnection("Server=(local);Database=Sharp;Trusted_Connection=yes");
SqlCommand myCommand =new SqlCommand(insertCmd,myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id",SqlDbType.VarChar,10));
myCommand.Parameters["@Id"].Value=id.Text;
myCommand.Parameters.Add(new SqlParameter("@Nickname",SqlDbType.VarChar,20));
myCommand.Parameters["@Nickname"].Value=nickname.Text;
myCommand.Parameters.Add(new SqlParameter("@Name",SqlDbType.VarChar,80));
myCommand.Parameters["@Name"].Value=name.Text;
myCommand.Parameters.Add(new SqlParameter("@Psw",SqlDbType.VarChar,16));
myCommand.Parameters["@Psw"].Value=psw.Text;
//myCommand.Parameters.Add(new SqlParameter("@city",SqlDbType.VarChar,50));
//myCommand.Parameters["@City"].Value=city.Text; myCommand.Connection.Open();//important
try
{
myCommand.ExecuteNonQuery();
//myCommand.ExecuteReader();
Message.Text="<b>Add Successfully </b>"+insertCmd;
}
catch(SqlException ee)
{
if(ee.Number==2627)
Message.Text="<b>ERROR: A record already exists with" +
" the same primary key"+insertCmd;
else
Message.Text = "ERROR: Could not add record, please " +
" ensure the fields are correctly filled out";
Message.Style["color"] = "red";
}
myCommand.Connection.Close();
BindGrid();
}
public void BindGrid()
{
SqlConnection myConnection =new SqlConnection("Server=(local);Database=Sharp ;Trusted_Connection=yes");
SqlDataAdapter myDataAdapter=new SqlDataAdapter("Select id ,nickname,name,psw,city from member",myConnection);
DataSet DS=new DataSet();
myDataAdapter.Fill(DS);
DG.DataSource=DS;
DG.DataBind();
} }
}
"@Nickname ,@Name,@Psw)";
String insertCmd="insert into member(Id,Nickname,Name,Psw) valuse(@Id,"+"@Nickname ,@Name,@Psw)";没有字段怎么添加
字段名,那上面那些@ID,@Nickname又是什么呢?
id,nickname都是控件的id来的,就是无法理解@ID了:)