简体   繁体   中英

error while updating a database in ASP.NET

I am having trouble updating an SQL database, the problem is not that it doesn't update at all, but that particular parameters are being updated while the others are not.

here is the code for updating the parameters:

string EditRequest = "UPDATE Requests SET Description = @Desc, BJustif = @Justif, Priority = @Priority, Requested_System = @Requested, Request_Status = @Stat WHERE";
    EditRequest += " Req_ID=@ID";

    SqlConnection Submit_conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["DBConn"].ConnectionString);
    SqlCommand Submit_comm = new SqlCommand(EditRequest, Submit_conn);


    Submit_comm.Parameters.AddWithValue("@ID", Request.QueryString["reqid"]);
    Submit_comm.Parameters.AddWithValue("@Desc", DescBox.Text);
    Submit_comm.Parameters.AddWithValue("@Justif", JustifBox.Text);
    Submit_comm.Parameters.AddWithValue("@Priority", PriorityList.SelectedValue);
    Submit_comm.Parameters.AddWithValue("@Requested", RelatedBox.Text);
    Submit_comm.Parameters.AddWithValue("@Stat", 1);

    Submit_conn.Open();

    Submit_comm.ExecuteNonQuery();

    Submit_comm.Dispose();

    Submit_comm = null;

    Submit_conn.Close();

    get_Description();

    Page.ClientScript.RegisterStartupScript(this.GetType(), "Refresh", "ReloadPage();", true);

this function is called by a button on a pop-up form which shows the parameters content that is being changed in a text box which is also used to submit the changes back to the database, but when I press submit, the parameters which are displayed on the form don't change, I can't find any problem wit the code, even though I've compared it to similar code which is working fine.

In case you need to, here is one of the text boxes I'm using to display and edit the content:

<asp:TextBox ID="JustifBox" TextMode="MultiLine" runat="server" Width="250" Height="50"></asp:TextBox>

What exactly is wrong with the code?

EDIT: I forgot to mention that when I traced the function, it appeared that the controls' content did not change when I submitted them, but were resubmitted as if they were unchanged in their original form.

You mentioned two problems here:

1) Fields in the database are not updated when the UPDATE is performed

2) The UI is not updated with the latest data

First tackle the SQL UPDATE query. What fields are not being updated? Copy paste the T-SQL query in the query analyzer and then check which field is being updated and which is NOT. Also, your code is OPEN to SQL injections so read about that and then adjust the code.

For the UI not being updated you need to see whether you are even populating the UI fields with the correct object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM