标题:c#.net 数据不能成功添加到数据库中 代码如下 求高手帮解决!!
只看楼主
水青玉
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-5-13
结帖率:0
已结贴  问题点数:10 回复次数:3 
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)
    {

    }
}
搜索更多相关主题的帖子: 数据库 成功 
2011-05-13 12:04
yinniannian
Rank: 9Rank: 9Rank: 9
来 自:河北省石家庄
等 级:蜘蛛侠
威 望:2
帖 子:256
专家分:1007
注 册:2011-5-13
得分:10 
我也是菜鸟,哪块的代码添加不进去数据呀?

代做小型软件。
QQ:449795473
2011-05-13 12:21
水青玉
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-5-13
得分:0 
回复 楼主 水青玉
刚刚修改了一下数据库  填是填进去了  但是后半部分的

            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();

            }
        }      

不起作用  添加成功后 直接变成白页
2011-05-13 12:55
yinniannian
Rank: 9Rank: 9Rank: 9
来 自:河北省石家庄
等 级:蜘蛛侠
威 望:2
帖 子:256
专家分:1007
注 册:2011-5-13
得分:0 
我有点不懂你的意思!asp网页,你每刷新一次页面,它都会从服务器上重新下载页面!

代做小型软件。
QQ:449795473
2011-05-13 20:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-339711-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038743 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved