当前位置:编程学习 > C#/ASP.NET >>

请高手帮忙,修改功能不能实现!

小弟刚学习.net,今天在设计网站中 有个修改功能不能实现,输入好后,点击修改,数据库无任何变化,请高手看看,帮忙解救,谢谢!
updata.aspx 代码为:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="KechengUpdate.aspx.cs" Inherits="HouAdmin_KechengUpdate" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <table style="background-image: url(../igm/在线考试后台1.jpg); width: 978px; height: 616px">
            <tr>
                <td colspan="3" style="height: 61px">
                     <br />
                </td>
                <td colspan="1" style="height: 61px">
                </td>
            </tr>
            <tr>
                <td style="width: 303px; height: 152px; text-align: left">
                     </td>
                <td style="vertical-align: top; width: 128px; height: 152px; text-align: left">
                     
                    <asp:TreeView ID="TreeView1" runat="server" ImageSet="Simple" NodeIndent="10" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"
                        ShowLines="True">
                        <ParentNodeStyle Font-Bold="False" />
                        <HoverNodeStyle Font-Underline="True" ForeColor="#DD5555" />
                        <SelectedNodeStyle Font-Underline="True" ForeColor="#DD5555" HorizontalPadding="0px"
                            VerticalPadding="0px" />
                        <Nodes>
                            <asp:TreeNode Expanded="False" NavigateUrl="~/HouAdmin/admin.aspx" Text="管理员" Value="新建节点">
                            </asp:TreeNode>
                            <asp:TreeNode Expanded="False" NavigateUrl="~/HouAdmin/zhuce_stu.aspx" Text="管理注册学生"
                                Value="管理注册学生"></asp:TreeNode>
                            <asp:TreeNode Expanded="False" NavigateUrl="~/HouAdmin/zhuanye_xinxi.aspx" Text="管理专业信息"
                                Value="管理专业信息"></asp:TreeNode>
                            <asp:TreeNode Expanded="False" NavigateUrl="~/HouAdmin/kecheng_xinxi.aspx" Text="管理课程信息"
                                Value="管理课程信息"></asp:TreeNode>
                            <asp:TreeNode Expanded="False" NavigateUrl="~/HouAdmin/taoti_xinxi.aspx" Text="管理套题信息"
                                Value="管理套题信息"></asp:TreeNode>
                            <asp:TreeNode NavigateUrl="~/HouAdmin/kaosheng_chengji.aspx" Text="管理考生成绩" Value="管理考生成绩">
                            </asp:TreeNode>
                            <asp:TreeNode Expanded="False" NavigateUrl="~/HouAdmin/kaoshi_timu.aspx" Text="管理考试题目"
                                Value="管理考试题目"></asp:TreeNode>
                            <asp:TreeNode Expanded="False" NavigateUrl="~/HouAdmin/insert_shiti.aspx" Text="添加试题"
                                Value="添加试题"></asp:TreeNode>
                            <asp:TreeNode Text="退出系统" Value="退出系统"></asp:TreeNode>
                        </Nodes>
                        <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="0px"
                            NodeSpacing="0px" VerticalPadding="0px" />
                    </asp:TreeView>
                </td>
                <td style="width: 729px; height: 152px">
                    <table>
                        <tr>
                            <td align="right" style="width: 100px">
                                <asp:Label ID="Label1" runat="server" Font-Size="9pt" Text="课程名称:"></asp:Label></td>
                            <td style="width: 100px">
                                <asp:TextBox ID="txtLessName" runat="server" Font-Size="9pt"></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td align="right" style="width: 100px">
                                <asp:Label ID="Label2" runat="server" Font-Size="9pt" Text="所属专业:"></asp:Label></td>
                            <td style="width: 100px">
                                <asp:DropDownList ID="ddlProfession" runat="server" Font-Size="9pt" Width="132px">
                                </asp:DropDownList></td>
                        </tr>
                        <tr>
                            <td style="width: 100px">
                            </td>
                            <td style="width: 100px; text-align: center">
                                <asp:Button ID="btnModify" runat="server" Font-Size="9pt" OnClick="Button1_Click"
                                    Text="修改" />
                                <asp:Button ID="btnBack" runat="server" Font-Size="9pt" OnClick="Button2_Click" Text="返回" /></td>
                        </tr>
                    </table>
                </td>
                <td style="width: 729px; height: 152px">
                </td>
            </tr>
            <tr>
                <td style="width: 303px; height: 152px; text-align: left">
                </td>
                <td style="vertical-align: top; width: 128px; height: 152px; text-align: left">
                </td>
                <td style="width: 729px; height: 152px">
                </td>
                <td style="width: 729px; height: 152px">
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>


updata.aspx.cs 代码如下:
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 HouAdmin_KechengUpdate : System.Web.UI.Page
{
    Datacon dataconn = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dataconn.ecDropDownList(ddlProfession, "select * from tb_Profession", "Name", "id");
            SqlConnection con = dataconn.getcon();
            con.Open();
            SqlDataAdapter mydataadapter = new SqlDataAdapter("select * from tb_Lesson where ID="
                + Request["id"], con);
            DataSet mydataset = new DataSet();
            mydataadapter.Fill(mydataset, "tb_lesson");
            DataRowView rowview = mydataset.Tables["tb_lesson"].DefaultView[0];
            this.txtLessName.Text = Convert.ToString(rowview["Name"]);
            ddlProfession.Text = Convert.ToString(rowview["ofProfession"]);
            con.Close();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string getid = Request.QueryString["ID"].ToString();
        dataconn.eccom("update tb_Lesson set Name='" + this.txtLessName.Text + "',ofProfession='"
            + ddlProfession.SelectedIndex + "' where  ID='" + getid + "'");
        Page.Response.Redirect("kecheng_xinxi.aspx"); 
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Page.Response.Redirect("kecheng_xinxi.aspx");
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (TreeView1.SelectedNode.Text == "退出系统")
        {
            Response.Write("<script lanuage=javascript>window.close();location='javascript:history.go(-1)'</script>");
        }
    }
}


--------------------编程问答-------------------- 补充一句,数据可以读出来,就是修改不了,估计是updata的时候出错了! --------------------编程问答-------------------- 1、先确认下: 【("update tb_Lesson set Name='" + this.txtLessName.Text + "',ofProfession='" + ddlProfession.SelectedIndex + "' where  ID='" + getid + "'"); 】生成的SQL语句是否正确!

2、若SQL语句正确,在看看dataconn.eccom是否对,这个应该是你封装的函数吧,看看是否有问题。

基本上这2个步骤走下来,问题就差不多能够解决了。(尤其注意第一条,那个SQL是否正确) --------------------编程问答-------------------- 再刷新一下页面,看看update语句是否实现。
如果实现了,则说明更新操作没有问题,则在Response.Redirect之前添加代码
SqlConnection con = dataconn.getcon(); 
            con.Open(); 
            SqlDataAdapter mydataadapter = new SqlDataAdapter("select * from tb_Lesson where ID=" 
                + Request["id"], con); 
            DataSet mydataset = new DataSet(); 
            mydataadapter.Fill(mydataset, "tb_lesson"); 
            DataRowView rowview = mydataset.Tables["tb_lesson"].DefaultView[0]; 
            this.txtLessName.Text = Convert.ToString(rowview["Name"]); 
            ddlProfession.Text = Convert.ToString(rowview["ofProfession"]); 
            con.Close(); 
--------------------编程问答--------------------  dataconn.eccom("update tb_Lesson set Name='" + this.txtLessName.Text + "',ofProfession='" 
            + ddlProfession.SelectedIndex + "' where  ID='" + getid + "'"); 

在这句话之前,加上:
string updateSql=("update tb_Lesson set Name='" + this.txtLessName.Text + "',ofProfession='" 
            + ddlProfession.SelectedIndex + "' where  ID='" + getid + "'";
Response.Write(updateSql);
然后去页面上将得到的sql语句去数据库中执行一下,如果无数据更新,则说明程序本身没问题,是你SQL语句有问题

在数据库中执行select * from tb_Lesson where ID='" + getid + "'"看有没有数据 --------------------编程问答-------------------- 谢谢高手们指点,查出问题来了。dataconn.eccom封装函数没问题,因为其他页面也调用都没出问题,是SQL语句有问题。

在dataconn.eccom("update tb_Lesson set Name='" + this.txtLessName.Text + "',ofProfession='" 
            + ddlProfession.SelectedIndex + "' where  ID='" + getid + "'"); 
之前加上
string updateSql=("update tb_Lesson set Name='" + this.txtLessName.Text + "',ofProfession='" 
            + ddlProfession.SelectedIndex + "' where  ID='" + getid + "'"; 
Response.Write(updateSql); 
到页面上将得到的sql语句去数据库中执行了一下,发现错误为:消息 547,级别 16,状态 0,第 1 行
UPDATE 语句与 FOREIGN KEY 约束"FK_tb_Lesson_tb_Profession"冲突。该冲突发生于数据库"db_Examination",表"dbo.tb_Profession", column 'ID'。
语句已终止。

表tb_Profession 中字段有ID(自动编号) Name(专业名称)
表tb_Lesson  中的字段ofProfession 约束于tb_Profession 表中的ID
因为前面对tb_Profession 表中的数据进行了删除操作,因为ID则不从0或1 开始,而ddlProfession.SelectedIndex下拉菜单中得到的数据是从0开始的(下拉菜单第一个数据 编号为0  第二哥特编号为1  以此类推。)
所以导致了约束冲突,请问应如何改?谢谢高手们。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,