动态更新数据库记录

时间:2014-04-08 03:26:28

标签: c# sql-server button executenonquery

它显示没有错误,除非我添加一个记录,它进入捕获异常,这给我连接问题作为输出。我尝试了很多,但无法找到我的错误。这是我的代码:

public partial class Default2 : System.Web.UI.Page
{
private string conStr = WebConfigurationManager.ConnectionStrings["StudentConnectionString1"].ConnectionString;

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        FillList();
        BtnsActive(false, true, false, false, false, false);

    }
}

protected void BtnsActive(bool a, bool b, bool c, bool d, bool e, bool f)
{
    Panel2.Enabled = a;
    btnAdd.Enabled = b;
    btnInsert.Enabled = c;
    btnEdit.Enabled = d;
    btnUpdate.Enabled = e;
    btnDelete.Enabled = f;

}
protected void FillList()
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Select * from Student order by StudId", con);
    SqlDataReader reader;
    DropDownList1.Items.Clear();
    try
    {
        con.Open();
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            ListItem newItem = new ListItem();
            newItem.Text = reader["StudId"] + "," + reader["StudFirstName"];
            newItem.Value = reader["StudId"].ToString();
            DropDownList1.Items.Add(newItem);
        }
    }
    catch (Exception er)
    {

        Response.Write("<script language='javascript'>alert('Connection Problem');</script>");
    }
    finally
    {
        con.Close();

    }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Select * from Student where StudId='" +  DropDownList1.SelectedValue + "'", con);
    SqlDataReader reader;

    try
    {
        con.Open();
        reader = cmd.ExecuteReader();
        reader.Read();

        TextBox1.Text = reader["StudId"].ToString();
        TextBox2.Text = reader["StudFirstName"].ToString();
        CheckBox1.Checked = (bool)reader["Library"];
    }
    catch (Exception er)
    {

        Response.Write("<script language='javascript'>alert('Connection Problem');</script>");
    }
    finally
    {
        con.Close();
        BtnsActive(false, true, false, true, false, true);
    }
}
protected void btnAdd_Click(object sender, EventArgs e)
{
    BtnsActive(true, false, true, false, false, false);
    TextBox1.Text = "";

    TextBox2.Text = "";
    CheckBox1.Checked = false;
}
protected void btnInsert_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Insert into Student (StudId,StudFirstName,Library) values(@sid,@name,@library)", con);
    cmd.Parameters.AddWithValue("@sid", TextBox1.Text);
    cmd.Parameters.AddWithValue("@name", TextBox2.Text);
    cmd.Parameters.AddWithValue("@library", CheckBox1.Checked);

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        Response.Write("<script language='javascript'>alert('Record has been added.');</script>");

    }
    catch (Exception er)
    {

        Response.Write("<script language='javascript'>alert('Connection Problem');</script>");
    }
    finally
    {
        con.Close();
        BtnsActive(false, true, false, false, false, false);
        FillList();
    }
}
protected void btnEdit_Click(object sender, EventArgs e)
{
    BtnsActive(true, false, false, false, true, false);
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Update Student set StudId=@id,StudFirstName=@name,Library=@library) where StudId=@oldId", con);
    cmd.Parameters.AddWithValue("@id", TextBox1.Text);
    cmd.Parameters.AddWithValue("@name", TextBox2.Text);
    cmd.Parameters.AddWithValue("@library", CheckBox1.Checked);
    cmd.Parameters.AddWithValue("@oldId", DropDownList1.SelectedValue);
    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        Response.Write("<script language='javascript'>alert('The Record has been Updated.');</script>");

    }
    catch (Exception er)
    {

        Response.Write("<script language='javascript'>alert('Connection Problem');</script>");
    }
    finally
    {
        con.Close();
        BtnsActive(false, true, false, false, false, false);
        FillList();
    }
}
protected void btnDelete_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand cmd = new SqlCommand("Delete from Student where StudId=@id", con);
    cmd.Parameters.AddWithValue("@id", TextBox1.Text);

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        Response.Write("<script language='javascript'>alert('The Record has been Deleted.');</script>");

    }
    catch (Exception er)
    {

        Response.Write("<script language='javascript'>alert('Connection Problem');</script>");
    }
    finally
    {
        con.Close();
        BtnsActive(false, true, false, false, false, false);
        FillList();
    }

}
}

这是我的设计代码很简单..

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs"     Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

    <asp:Panel ID="Panel1" runat="server" BackColor="#CC3300">
        Stud ID:<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        <br />
        <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add" />
        <asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />
        <asp:Button ID="btnEdit" runat="server" OnClick="btnEdit_Click" Text="Edit" />
        <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" />
        <asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete" />
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server" BackColor="#FF9933">
        <br />
        <br />
        Stud ID:
        <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
        <br />
        Name:
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        Library :
        <asp:CheckBox ID="CheckBox1" runat="server" />
    </asp:Panel>

</div>
</form>
</body>
</html>

0 个答案:

没有答案