c#.net 数据不能成功添加到数据库中 代码如下 求高手帮解决!!
using System;using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Class_tianjia : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd, cmd1;
SqlDataReader dr,dr1;
sqldata sqldata;
string strsql, strsql1;
protected void Page_Load(object sender, EventArgs e)
{
TreeView1.ExpandAll();//展开树的所有节点
/* if (!this.IsPostBack)
{
if (Session["name"] == null)
{
Response.Redirect("login.aspx");
}
else
{
this.lblname.Text = Convert.ToString(Session["name"]);
}
}*/
if (!IsPostBack)
{
con = sqldata.createcon();
con.Open();
strsql = "select Col_name from College ";
cmd = new SqlCommand(strsql, con);
dr = cmd.ExecuteReader();
this.ddlxueyuan.DataSource = dr;
this.ddlxueyuan.DataTextField = "Col_name";
this.ddlxueyuan.DataBind();
con.Close();
con = sqldata.createcon();
con.Open();
strsql1 = "select Pro_name from Professional ";
cmd1 = new SqlCommand(strsql1, con);
dr1 = cmd1.ExecuteReader();
this.ddlzhuanye.DataSource = dr1;
this.ddlzhuanye.DataTextField = "Pro_name";
this.ddlzhuanye.DataBind();
this.ddlzhuanye.Items.Add("请选择您的专业名称");
this.ddlzhuanye.SelectedIndex = this.ddlzhuanye.Items.Count - 1;
this.ddlxueyuan.Items.Add("请选择您的学院名称");
this.ddlxueyuan.SelectedIndex = this.ddlxueyuan.Items.Count - 1;
con.Close();
}
}
protected void btnyes_Click(object sender, EventArgs e)
{
//校验数据
string Col_name, Pro_name, Cla_name, Cla_teacher,Cla_else;
Col_name = this.ddlxueyuan.Text;
Pro_name = this.ddlzhuanye.Text;
Cla_name = this.txtname.Text;
Cla_teacher = this.txtteacher.Text;
Cla_else = this.txtelse.Text;
//连接数据库
SqlConnection con = new SqlConnection();
con = sqldata.createcon();
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
= CommandType.Text;
= "select * from Class where Cla_name='"+this .txtname .Text +"'";
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{
lbltishi.Text = "该班级信息已存在!";
}
else
{
dr.Close();
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = con;
= CommandType.Text;
= "insert into Class (Col_name, Pro_name, Cla_name, Cla_teacher,Cla_else) values ('" +this .ddlxueyuan .SelectedItem .Value .ToString ()+ "','" +this .ddlzhuanye .SelectedItem .Value .ToString () + "','" + txtname .Text + "','"+txtteacher .Text +"','"+txtelse .Text +"')";
try
{
cmd1.ExecuteNonQuery();
Response.Write("<script language=javascript>alter('添加信息成功!');history.go(-1);</script>");
// btnchakan.Enabled = true;
Response.End();
}
catch (SqlException s)
{
Response.Write("<script language=javascript>alter('添加信息失败!');history.go(-1);</script>");
}
finally
{
con.Close();
}
}
}
protected void btnno_Click(object sender, EventArgs e)
{
this.txtname.Text="";
this.txtteacher.Text="";
this.txtelse.Text="";
}
protected void btnchakan_Click(object sender, EventArgs e)
{
}
}