更新表单不更新详细信息

时间:2013-03-15 17:24:30

标签: c# asp.net sql sql-update

我创建了一个简单的表单来显示用户详细信息,其中包含以下字段:

First Name
Surname
Team Name
Team Stadium
Email
Receive Reminder
Receive Summary

我检索登录用户的详细信息,并允许编辑字段进行更新。但是,当触发更新按钮的单击事件时,字段将返回其原始值。

这是我在C#中的代码

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["RaiseFantasyLeagueConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand("[dbo].[GetUsersProfile]", conn);
    cmd.CommandType = CommandType.StoredProcedure;

    string userId = Membership.GetUser().ProviderUserKey.ToString();
    SqlParameter userIDParam = new SqlParameter("@userId", userId);

    cmd.Parameters.Add(userIDParam);

    conn.Open();
    SqlDataReader dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    while (dReader.Read())
    {
        FirstName.Text = (dReader["ManagerFirstName"].ToString());
        Surname.Text = (dReader["ManagerSurname"].ToString());
        TeamName.Text = (dReader["TeamName"].ToString());
        StadiumName.Text = (dReader["TeamStadium"].ToString());
        Email.Text = (dReader["Email"].ToString());
        Reminder.Checked = (bool)dReader["RecieveReminder"];
        Prediction.Checked = (bool)dReader["RecieveSummary"];
    }
    dReader.Close();
    conn.Close();
}

protected void UpdateDetailsButton_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["RaiseFantasyLeagueConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand("[dbo].[UpdateUserDetails]", conn);
    cmd.CommandType = CommandType.StoredProcedure;

    string userId = Membership.GetUser().ProviderUserKey.ToString();
    SqlParameter userIDParam = new SqlParameter("@userId", userId);

    TextBox FirstNameTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMain").FindControl("FirstName");
    SqlParameter FirstNameParam = new SqlParameter("@ManagerFirstName", FirstNameTextBox.Text);

    TextBox SurnameTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMain").FindControl("Surname");
    SqlParameter SurnameParam = new SqlParameter("@ManagerSurname", SurnameTextBox.Text);

    TextBox TeamNameTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMain").FindControl("TeamName");
    SqlParameter TeamNameParam = new SqlParameter("@TeamName", TeamNameTextBox.Text);

    TextBox TeamStadiumNameTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMain").FindControl("StadiumName");
    SqlParameter TeamStadiumNameParam = new SqlParameter("@TeamStadium", TeamStadiumNameTextBox.Text);

    TextBox EmailTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMain").FindControl("Email");
    SqlParameter EmailParam = new SqlParameter("@Email", EmailTextBox.Text);

    SqlParameter RecieveReminderParam = new SqlParameter("@RecieveReminder", 1);

    SqlParameter RecieveSummaryParam = new SqlParameter("@RecieveSummary", 1);
    cmd.Parameters.Add(userIDParam);
    cmd.Parameters.Add(FirstNameParam);
    cmd.Parameters.Add(SurnameParam);
    cmd.Parameters.Add(TeamNameParam);
    cmd.Parameters.Add(TeamStadiumNameParam);
    cmd.Parameters.Add(EmailParam);
    cmd.Parameters.Add(RecieveReminderParam);
    cmd.Parameters.Add(RecieveSummaryParam);

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}

对于ASP

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderMain" runat="server">
<div class="row">
    <div class="span12">
        <h3>
            My Profile</h3>
    </div>
</div>
<div class="row">
    <div class="span2">
        <label class="control-label" for="firstNameTextBox">
            First Name:</label>
    </div>
    <div class="span3">
        <asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
    </div>
</div>
<div class="row">
    <div class="span2">
        <label class="control-label" for="surnameTextBox" >
            Surname:</label>
    </div>
    <div class="span3">
        <asp:TextBox ID="Surname" runat="server"></asp:TextBox>
    </div>
</div>
<div class="row">
    <div class="span2">
        <label class="control-label" for="teamNameTextBox">
            Team Name:</label>
    </div>
    <div class="span3">
        <asp:TextBox ID="TeamName" runat="server"></asp:TextBox>
    </div>
</div>
<div class="row">
    <div class="span2">
        <label class="control-label" for="stadiumNameTextBox">
            Stadium Name:</label>
    </div>
    <div class="span3">
        <asp:TextBox ID="StadiumName" runat="server"></asp:TextBox>
    </div>
</div>
<div class="row">
    <div class="span2">
        <label class="control-label" for="emailTextBox">
            Email Address:</label>
    </div>
    <div class="span3">
        <asp:TextBox ID="Email" runat="server"></asp:TextBox>
    </div>
</div>
<div class="row">
    <div class="span2">
        <label class="control-label" for="reminderCheckBox">
            Recieve Reminder Email:</label>
    </div>
    <div class="span3">
        <input type="checkbox" id="Reminder" runat="server"/>
    </div>
</div>
<div class="row">
    <div class="span2">
        <label class="control-label" for="predictionCheckBox">
            Recieve Prediction Email:</label>
    </div>
    <div class="span3">
        <input type="checkbox" id="Prediction" runat="server"/>
    </div>
</div>
<div class="row">
    <div class="span5">
        <asp:Button ID="UpdateDetailsButton" 
            CssClass="btn btn-primary rightAlignButton" runat="server" Text="Confirm" 
            onclick="UpdateDetailsButton_Click" />
    </div>
</div>

我已尝试在SQL Management Studio中使用存储过程并按预期更新,因此出于某种原因,单击更新时字段不会读取更新的值。

任何建议都会很棒,谢谢!!

2 个答案:

答案 0 :(得分:1)

在Page_Load事件中使用IsPostBack

if(!IsPostBack)
{
        SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["RaiseFantasyLeagueConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("[dbo].[GetUsersProfile]", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        string userId = Membership.GetUser().ProviderUserKey.ToString();
        SqlParameter userIDParam = new SqlParameter("@userId", userId);

        cmd.Parameters.Add(userIDParam);

        conn.Open();
        SqlDataReader dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (dReader.Read())
        {
            FirstName.Text = (dReader["ManagerFirstName"].ToString());
            Surname.Text = (dReader["ManagerSurname"].ToString());
            TeamName.Text = (dReader["TeamName"].ToString());
            StadiumName.Text = (dReader["TeamStadium"].ToString());
            Email.Text = (dReader["Email"].ToString());
            Reminder.Checked = (bool)dReader["RecieveReminder"];
            Prediction.Checked = (bool)dReader["RecieveSummary"];
        }
        dReader.Close();
        conn.Close();
}

答案 1 :(得分:0)

您应该只在第一次从数据库加载数据,因此您必须检查它是否不是Postback。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        LoadDataFromDatabase();
    }
}

private void LoadDataFromDatabase()
{
    SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["RaiseFantasyLeagueConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand("[dbo].[GetUsersProfile]", conn);
    cmd.CommandType = CommandType.StoredProcedure;

    string userId = Membership.GetUser().ProviderUserKey.ToString();
    SqlParameter userIDParam = new SqlParameter("@userId", userId);

    cmd.Parameters.Add(userIDParam);

    conn.Open();
    SqlDataReader dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    while (dReader.Read())
    {
        FirstName.Text = (dReader["ManagerFirstName"].ToString());
        Surname.Text = (dReader["ManagerSurname"].ToString());
        TeamName.Text = (dReader["TeamName"].ToString());
        StadiumName.Text = (dReader["TeamStadium"].ToString());
        Email.Text = (dReader["Email"].ToString());
        Reminder.Checked = (bool)dReader["RecieveReminder"];
        Prediction.Checked = (bool)dReader["RecieveSummary"];
    }
    dReader.Close();
    conn.Close();
}