该页面添加数据的时候总是提示添加失败,谁能帮忙看看是哪里出错了!谢谢aspx代码:
<%@ Page language="c#" Inherits="pjtest.maintea" CodeFile="maintea.aspx.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>maintea</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<TABLE style="WIDTH: 851px; HEIGHT: 490px" cellSpacing="0" cellPadding="0" width="851"
align="center" border="0">
<TR>
<TD id="leftFrame" style="HEIGHT: 513px" vAlign="middle" noWrap align="center" width="1"
height="513" rowSpan="2" name="leftFrame"><FONT face="宋体"></FONT></TD>
<TD style="WIDTH: 146px; HEIGHT: 513px" width="146" bgColor="#ccccff" rowSpan="2"><FONT face="宋体">
<P align="center">
<asp:HyperLink id="HyperLink5" ToolTip="回到后台管理首页" NavigateUrl="main.aspx" runat="server">后台管理首页</asp:HyperLink></P>
<P align="center"><asp:hyperlink id="Hyperlink4" runat="server" NavigateUrl="mainStu.aspx" ToolTip="进入对学生信息的添加、删除、修改">进入学生管理</asp:hyperlink></P>
</FONT>
<P align="center">
<asp:HyperLink id="Hyperlink6" runat="server" ToolTip="进入对教师信息的添加、删除、修改" NavigateUrl="maintea.aspx">进入教师管理</asp:HyperLink></P>
<P align="center">
<asp:HyperLink id="Hyperlink7" runat="server" NavigateUrl="pjItem.aspx" ToolTip="进入用于评教信息的设置">进入评教管理</asp:HyperLink></P>
<P align="center">
<asp:HyperLink id="Hyperlink8" runat="server" ToolTip="查看此次评教的结果,对数据进行分析" NavigateUrl="result.aspx">评教结果汇总</asp:HyperLink></P>
<P align="center">
<asp:HyperLink id="HyperLink2" ToolTip="设置不可信的模型" NavigateUrl="pjshezhi.aspx" runat="server">评教自定义设置</asp:HyperLink></P>
<P align="center"><FONT face="宋体">
<asp:HyperLink id="HyperLink1" NavigateUrl="index.aspx" runat="server" ForeColor="Black">退出管理</asp:HyperLink></FONT></P>
<P align="center"><FONT face="宋体"></FONT>&nbsp;</P>
</TD>
<TD height="70" bgcolor="#ffffff" style="WIDTH: 702px">
<P align="center"><FONT face="仿宋_GB2312" size="5"><U>欢迎进入评教系统后台管理</U></FONT>&nbsp;
<asp:Label id="Label1" runat="server" BackColor="Transparent" Font-Bold="True">教师信息管理页面</asp:Label></P>
</TD>
</TR>
<TR>
<TD width="702" height="441" bgcolor="#ffffff" style="WIDTH: 702px; HEIGHT: 441px">
<P align="center"><FONT face="宋体"></FONT>&nbsp;</P>
</TD>
</TR>
</TABLE>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="dgTea" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 144px" runat="server"
AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" BackColor="White" BorderWidth="1px"
BorderStyle="None" BorderColor="#3366CC" Width="656px">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle Height="20px" ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" Height="25px" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="teaNum" ReadOnly="True" HeaderText="教师编号">
<HeaderStyle HorizontalAlign="Center" Width="68px" VerticalAlign="Middle"></HeaderStyle>
<ItemStyle Wrap="False" HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="teaName" HeaderText="教师姓名">
<HeaderStyle HorizontalAlign="Center" Width="68px" VerticalAlign="Middle"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="teaPwd" HeaderText="登录密码">
<HeaderStyle HorizontalAlign="Center" Width="68px" VerticalAlign="Middle"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="teaItem" HeaderText="所授课程">
<HeaderStyle HorizontalAlign="Center" Width="68px" VerticalAlign="Middle"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="teaClass" HeaderText="所教班级">
<HeaderStyle HorizontalAlign="Center" Width="68px" VerticalAlign="Middle"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundColumn>
<asp:ButtonColumn Text="删除" CommandName="Delete">
<HeaderStyle HorizontalAlign="Center" Width="70px" VerticalAlign="Middle"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:ButtonColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑">
<HeaderStyle HorizontalAlign="Center" Width="100px" VerticalAlign="Middle"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:EditCommandColumn>
</Columns>
<PagerStyle NextPageText="下一页" PrevPageText="上一页" HorizontalAlign="Left" ForeColor="#003399"
BackColor="#99CCCC"></PagerStyle>
</asp:datagrid><asp:textbox id="txtNum2" style="Z-INDEX: 106; LEFT: 232px; POSITION: absolute; TOP: 120px" runat="server"
Width="80px"></asp:textbox><asp:textbox id="txtName2" style="Z-INDEX: 102; LEFT: 320px; POSITION: absolute; TOP: 120px"
runat="server" Width="80px"></asp:textbox><asp:textbox id="txtPwd2" style="Z-INDEX: 103; LEFT: 408px; POSITION: absolute; TOP: 120px" runat="server"
Width="80px"></asp:textbox><asp:textbox id="txtItem" style="Z-INDEX: 104; LEFT: 496px; POSITION: absolute; TOP: 120px" runat="server"
Width="88px"></asp:textbox><asp:button id="btnADD" style="Z-INDEX: 105; LEFT: 696px; POSITION: absolute; TOP: 120px" runat="server"
Text="添加" onclick="btnADD_Click"></asp:button><asp:textbox id="txtClass" style="Z-INDEX: 107; LEFT: 592px; POSITION: absolute; TOP: 120px"
runat="server" Width="88px"></asp:textbox></form>
</body>
</HTML>

解决方案 »

  1.   


    aspx.cs代码:using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data .SqlClient ;namespace pjtest
    {
    /// <summary>
    /// maintea 的摘要说明。
    /// </summary>
    public partial class maintea : System.Web.UI.Page
    {

    protected void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!this.IsPostBack )
    {
    this.BindToDataGrid ();
    }
    }
    private void BindToDataGrid()
    {
    try
    {
    SqlConnection con=DB.createCon ();
    SqlDataAdapter sda=new SqlDataAdapter ();
    sda.SelectCommand =new SqlCommand ("select * from tea",con);
    this.dgTea .DataKeyField ="teaNum";
    DataSet ds=new DataSet ();
    sda.Fill (ds,"tea");
    this.dgTea .DataSource =ds.Tables ["tea"];
    this.dgTea .DataBind ();
    }
    catch
    { }

    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.dgTea.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgTea_PageIndexChanged);
    this.dgTea.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTea_CancelCommand);
    this.dgTea.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTea_EditCommand);
    this.dgTea.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTea_UpdateCommand);
    this.dgTea.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTea_DeleteCommand);
    this.dgTea.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgTea_ItemDataBound); }
    #endregion private void dgTea_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    this.dgTea .CurrentPageIndex =e.NewPageIndex ;
    this.BindToDataGrid ();
    } private void dgTea_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string teaNum=this.dgTea .DataKeys [e.Item .ItemIndex ].ToString ();
    SqlConnection con=DB.createCon ();
    SqlCommand cmd= new SqlCommand ("delete from tea where teaNum='"+teaNum+"'",con);
    con.Open ();
    cmd.ExecuteNonQuery ();
    this.BindToDataGrid ();
    con.Close();
    } private void dgTea_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.dgTea .EditItemIndex =e.Item .ItemIndex ;
    this.BindToDataGrid ();
    } private void dgTea_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string teaNum=this.dgTea .DataKeys [e.Item .ItemIndex ].ToString ();
    string teaName= ((TextBox)(e.Item .Cells [1].Controls [0])).Text ;
    string teaPwd= ((TextBox)(e.Item .Cells [2].Controls [0])).Text ;
    string teaItem= ((TextBox)(e.Item .Cells [3].Controls [0])).Text ;
    string teaClass=((TextBox)(e.Item .Cells [4].Controls [0])).Text ; SqlConnection con=DB.createCon ();
    SqlCommand cmd=new SqlCommand ("update tea set teaName='"+teaName+"',teaPwd='"+teaPwd+"',teaItem='"+teaItem+"',teaClass='"+teaClass+"'where teaNum='"+teaNum+"'",con);
    con.Open();
    cmd.ExecuteNonQuery ();
     
    this.BindToDataGrid ();
    con.Close ();
    } private void dgTea_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.dgTea .EditItemIndex =-1;
    this.BindToDataGrid ();
    } protected void btnADD_Click(object sender, System.EventArgs e)
    {
    try
    {
                    SqlConnection con = DB.createCon ();
    string teaNum=this.txtNum2.Text  ;
    string teaName=this.txtName2.Text ;
    string teaClass=this.txtClass.Text  ;
    string teaPwd=this.txtPwd2.Text ;
    string teaItem=this.txtItem .Text ; if (teaNum=="")
    {
    Response.Write ("教师编号不能为空");

    }
    else
    {

    SqlCommand cmd=new SqlCommand ("insert into tea values('"+teaNum+"','"+teaName+"','"+teaPwd+"','"+teaItem+"','"+teaClass+"')",con);
    con.Open();
    cmd.ExecuteNonQuery ();
    this.BindToDataGrid ();
    con.Close ();
    Response.Write ("添加教师成功");
    this.txtNum2.Text ="";
    this.txtName2.Text ="";
    this.txtClass .Text ="";
    this.txtPwd2 .Text ="";
    this.txtItem .Text ="";
    }
    }
    catch
    {
    Response.Write ("添加失败,请检查约束项!");
    }
    } private void dgTea_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item .ItemType ==ListItemType.EditItem )
    {
    for(int i=0;i<e.Item .Cells .Count ;i++)
    {
    if(e.Item .Cells [i].Controls .Count >0)
    {
        TextBox t=(TextBox)(e.Item .Cells [i].Controls [0]);
    t.Width =80;
    }
    } }
    }
    }
    }
      

  2.   

    "insert into tea(teaNum,teaPwd,teaItem,teaClass) values('"+teaNum+"','"+teaName+"','"+teaPwd+"','"+teaItem+"','"+teaClass+"')"