2014-02-19 49 views
0

我有一個編輯帳戶頁面,用於預加載用戶帳戶信息,並且他們可以在需要時進行編輯。該代碼沒有顯示任何錯誤,但它始終運行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"); 
    } 
} 
+3

的是什麼**異常消息**? –

+0

它沒有向你顯示任何錯誤的原因是,你正在捕捉異常,使用'Response.Write'以一種甚至可能不會顯示在你的頁面上的方式編寫它們(嘗試圍繞'

{ex.ToString}
'然後繼續進行,好像什麼也沒有發生過 –

+0

它沒有給我任何異常消息,但只要代碼到達Response.Redirect(ViewAccount.aspx)它就會發送到catch – user2526337

回答

0

根據您的評論above

它不給我任何異常消息,但只要代碼獲取到的Response.Redirect(ViewAccount.aspx)它去捕捉

看起來這是你有

System.Threading.ThreadAbortExce錯誤ption:線程正在中止。

試圖改變這種

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); 
相關問題