更新查询无法在编辑帐户页面上工作

时间:2014-02-19 01:05:01

标签: c# asp.net linq

我有一个编辑帐户页面,可以预先加载用户帐户信息,他们可以根据需要进行编辑。代码没有显示任何错误,但它始终运行catch并且不保存新数据。这是代码。所以基本上查询不会更新记录。

    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master"     AutoEventWireup="true" CodeFile="EditAccount.aspx.cs" Inherits="EditAccount" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">


<table style="width: 100%">
        <tr>
            <td style="text-align: center">
                    User Information : Edit
            </td>
        </tr>
    </table>
    <table style="width:100%">
        <tr>
            <td style="width: 25%">
                <asp:Label ID="lblUsername" runat="server" Text="Username : "></asp:Label>
            </td>
            <td style="width: 75%">
                <asp:TextBox ID="txtUsername" runat="server" Text="test"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 25%">
                <asp:Label ID="lblFirstName" runat="server" Text="First Name : "></asp:Label>
            </td>
            <td style="width: 75%">
                <asp:TextBox ID="txtFirstName" runat="server" Text="test"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 25%">
                <asp:Label ID="lblLastName" runat="server" Text="Last Name : "></asp:Label>
            </td>
            <td style="width: 75%">
                <asp:TextBox ID="txtLastName" runat="server" Text="test"></asp:TextBox>
            </td>
        </tr>

        <tr>
            <td style="width: 25%">
                <asp:Label ID="lblAddress" runat="server" Text="Address : "></asp:Label>
            </td>
            <td style="width: 75%">
                <asp:TextBox ID="txtAddress" runat="server" Text="test"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 25%">
                <asp:Label ID="lblCity" runat="server" Text="City : "></asp:Label>
            </td>
            <td style="width: 75%">
                <asp:TextBox ID="txtCity" runat="server" Text="test"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 25%">
                <asp:Label ID="lblState" runat="server" Text="State : "></asp:Label>
            </td>
            <td style="width: 75%">
                <asp:DropDownList ID="ddState" OnSelectedIndexChanged="btnSubmit_Click" runat="server" DataSourceID="SqlDataSource1" DataTextField="State" DataValueField="State" Height="17px" Width="148px" >
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT [State] FROM [States]"></asp:SqlDataSource>
            </td>
        </tr>
        <tr>
            <td style="width: 25%">
                <asp:Label ID="lblZip" runat="server" Text="Zip : "></asp:Label>
            </td>
            <td style="width: 75%">
                <asp:TextBox ID="txtZip" runat="server" Text="test"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 25%">
                <asp:Label ID="lblPhone" runat="server" Text="Phone# : "></asp:Label>
            </td>
            <td style="width: 75%">
                <asp:TextBox ID="txtPhone" runat="server" Text="test"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 25%">
                <asp:Label ID="lblEmail" runat="server" Text="E-Mail : "></asp:Label>
            </td>
            <td style="width: 75%">
                <asp:TextBox ID="txtEmail" runat="server" Text="test"></asp:TextBox>
            </td>
        </tr>
    </table>
    <table style="width: 100%">
        <tr>
            <td style ="width: 14%">

                <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

            </td>
            <td>
                <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
            </td>
        </tr>
    </table>


</asp:Content>

这里是C#代码。单击“提交”按钮是查询无效。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.Configuration;

public partial class EditAccount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["New"] != null)
        {

            txtUsername.Text = Session["New"].ToString();
            OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            conn.Open();

            string getfirstname = "select UserFirstName from Users where Username = '" + Session["New"].ToString() + "'";
            OleDbCommand firstcmd = new OleDbCommand(getfirstname, conn);
            string firstName = firstcmd.ExecuteScalar().ToString();
            txtFirstName.Text = firstName;

            string getlastname = "select UserLastName from Users where Username = '" + Session["New"].ToString() + "'";
            OleDbCommand lastcmd = new OleDbCommand(getlastname, conn);
            string LastName = lastcmd.ExecuteScalar().ToString();
            txtLastName.Text = LastName;

            string getaddress = "select ShipAddress from Users where Username = '" + Session["New"].ToString() + "'";
            OleDbCommand addresscmd = new OleDbCommand(getaddress, conn);
            string Address = addresscmd.ExecuteScalar().ToString();
            txtAddress.Text = Address;

            string getcity = "select ShipCity from Users where Username = '" + Session["New"].ToString() + "'";
            OleDbCommand citycmd = new OleDbCommand(getcity, conn);
            string City = citycmd.ExecuteScalar().ToString();
            txtCity.Text = City;

            string getstate = "select ShipState from Users where Username = '" + Session["New"].ToString() + "'";
            OleDbCommand statecmd = new OleDbCommand(getstate, conn);
            string State = statecmd.ExecuteScalar().ToString();
            ddState.Text = State;

            string getzip = "select ShipZip from Users where Username = '" + Session["New"].ToString() + "'";
            OleDbCommand zipcmd = new OleDbCommand(getzip, conn);
            string Zip = zipcmd.ExecuteScalar().ToString();
            txtZip.Text = Zip;

            string getphone = "select UserPhone from Users where Username = '" + Session["New"].ToString() + "'";
            OleDbCommand phonecmd = new OleDbCommand(getphone, conn);
            string Phone = phonecmd.ExecuteScalar().ToString();
            txtPhone.Text = Phone;

            string getemail = "select UserEmail from Users where Username = '" + Session["New"].ToString() + "'";
            OleDbCommand emailcmd = new OleDbCommand(getemail, conn);
            string Email = emailcmd.ExecuteScalar().ToString();
            txtEmail.Text = Email;
            conn.Close();

            Response.Write(txtUsername.Text);

        }
        else
        {
            Response.Redirect("LogIn.aspx");
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = '" + Server.MapPath("App_Data/WSC_DB.mdb") + "'; Persist Security Info=False");

            using (OleDbCommand cmd = new OleDbCommand("update Users set UserFirstName = @FirstName, UserLastName = @LastName, ShipAddress = @Address, ShipCity = @City, ShipState = @State, ShipZip = @Zip, UserPhone = @Phone, UserEmail = @Email, UserName = @Username where UserName = '" + Session["New"].ToString() + "'", conn))
            {
                cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
                cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
                cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
                cmd.Parameters.AddWithValue("@City", txtCity.Text);
                cmd.Parameters.AddWithValue("@State", ddState.SelectedValue.ToString());
                cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
                cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
                cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
                cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                Response.Redirect("ViewAccount.aspx");
            }
        }
        catch (Exception ex)
        {
            Response.Write("Error: " + ex.ToString());
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewAccount.aspx");
    }
}

1 个答案:

答案 0 :(得分:0)

根据您的评论above

  

它没有给我任何异常消息,但是一旦代码到达Response.Redirect(ViewAccount.aspx),它就会转到catch

看起来这是你得到的错误

  

System.Threading.ThreadAbortException:线程正在中止。

尝试更改此

Response.Redirect("ViewAccount.aspx");

到这个

Response.Redirect("ViewAccount.aspx", false);

并将该行移出using

using (OleDbCommand cmd = new OleDbCommand("update Users set UserFirstName = @FirstName, UserLastName = @LastName, ShipAddress = @Address, ShipCity = @City, ShipState = @State, ShipZip = @Zip, UserPhone = @Phone, UserEmail = @Email, UserName = @Username where UserName = '" + Session["New"].ToString() + "'", conn))
{
    cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
    cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
    cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
    cmd.Parameters.AddWithValue("@City", txtCity.Text);
    cmd.Parameters.AddWithValue("@State", ddState.SelectedValue.ToString());
    cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
    cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
    cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
    cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}

Response.Redirect("ViewAccount.aspx", false);