简体   繁体   English

通过数据库中的ID更新信息-ASP.NET/C#

[英]Updating information by ID in a database - ASP.NET/C#

Thanks for looking at my question in advanced. 感谢您仔细查看我的问题。

I am trying to update information in a database from a save button. 我正在尝试通过保存按钮更新数据库中的信息。

At the moment I have got it saving, but it saves the update as a new row. 目前,我已经保存了它,但是将更新保存为新行。

I have got the ID of the row that I wish to change but I am struggling to get it to work. 我已经有了要更改的行的ID,但是我正在努力使其正常工作。

ASP.NET: ASP.NET:

<asp:Button runat="server" ID="btnSave" CssClass="btnSubmit" Text="Save" OnClick="btnSave_Click" />

C#: C#:

protected void btnSave_Click(object sender, EventArgs e)
{
    int ItemID = 0;
    var Item = Placements.GetPlacementByExpiry(ItemID, false);
    if (Item == null)
    {
        Item = new Placements();
    }

    Item.CategoryID = Convert.ToInt32(ddlCategory.SelectedValue);
    Item.Title = txtTitle.Text;
    Item.Company = txtCompany.Text;
    Item.Location = txtLocation.Text;
    Item.Duration = txtDuration.Text;
    Item.SalaryFrom = Convert.ToDecimal(txtSalaryFrom.Text);
    Item.SalaryTo = Convert.ToDecimal(txtSalaryTo.Text);
    Item.SalaryType = ddlSalaryType.Text;
    Item.Description = txtDescription.Text;
    Item.Responsibilities = txtResponsibilities.Text;
    Item.Requirements = txtRequirements.Text;
    DateTime ExpiryDate = DateTime.ParseExact(txtExpiryDate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    Item.ExpiryDate = ExpiryDate;
    Item.DateCreated = DateTime.Now;

    if (Item.ID == 0)
    {
        Placements.CreatePlacement(Item);
    }
    else
    {
        Placements.UpdatePlacement(Item);
    }

    mvEdit.SetActiveView(vwList);
}

I have tried putting a command argument on the button and changed it in the back end, but receive the following error: 我尝试将命令参数放在按钮上,并在后端将其更改,但是收到以下错误:

Error   1   No overload for 'btnSave_Click' matches delegate 'System.EventHandler'  C:\Users\laura\Documents\Visual Studio 2010\WebSites\ThirdYearProject\tools\controls\placements\edit.ascx   239

I've been trying to fix it for a while now but I'm not sure what I'm doing wrong! 我已经尝试修复了一段时间,但不确定自己做错了什么! Any help would be appreciated. 任何帮助,将不胜感激。

I think Item.ID == 0 is not 0 either null or some other values. 我认为Item.ID == 0不为0或为null或其他一些值。 Please check the value of that. 请检查该值。

Is it a typo? 是错字吗? Shouldn't this line 这行不应该

btnSave_Click+= new System.EventHandler(this.btnSave_Click);

be moved to the possibly the Page_Load event or even the Pre_Init? 被移动到可能的Page_Load事件甚至是Pre_Init?

And I think below answer should be help to you 我认为下面的答案应该对您有帮助

Sample program Event bubbling in Asp.Net Asp.Net中的示例程序事件冒泡

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

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