简体   繁体   English

ASP.NET中更新数据库时出错

[英]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. 我在更新SQL数据库时遇到问题,问题不在于它根本不更新,而是特定参数正在更新,而其他参数则没有。

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. 此功能由弹出式窗口上的按钮调用,该按钮在文本框中显示正在更改的参数内容,该文本框也用于将更改提交回数据库,但是当我按Submit时,显示的参数在表格上保持不变,即使我已将其与工作正常的类似代码进行了比较,我也找不到任何问题。

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 1)执行更新时,数据库中的字段不会更新

2) The UI is not updated with the latest data 2)用户界面未使用最新数据更新

First tackle the SQL UPDATE query. 首先解决SQL UPDATE查询。 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. 将T-SQL查询复制粘贴到查询分析器中,然后检查哪个字段正在更新,哪些不是。 Also, your code is OPEN to SQL injections so read about that and then adjust the code. 另外,您的代码对SQL注入是开放的,因此请阅读有关内容,然后调整代码。

For the UI not being updated you need to see whether you are even populating the UI fields with the correct object. 对于未更新的UI,您需要查看您是否甚至还在用正确的对象填充UI字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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