简体   繁体   English

ASP.net无法在SQL Server中更新列

[英]ASP.net unable to UPDATE column in SQL Server

I'm creating an online application where I'm trying to update data through asp.net winforms. 我正在创建一个在线应用程序,我正在尝试通过asp.net winforms更新数据。 I have a textbox named tbCompanyName and a column in my database named companyName , in table tblCompanySetup . 我在表tblCompanySetup有一个名为tbCompanyNametextbox和一个名为companyName数据库中的列。

I'm SELECTING previous data from column perfectly, but when I try to update data, it is not updating data, although don't showing any errors. 我正在从列中完美地选择以前的数据,但是当我尝试更新数据时,尽管没有显示任何错误,但它并未更新数据。 I have worked in C# desktop application, but I'm completely newbie in ASP.NET. 我曾在C#桌面应用程序中工作,但在ASP.NET中我是新手。

I think there may be any AUTOPOSTBACK issues, but I don't know how to resolve. 我想可能有任何AUTOPOSTBACK问题,但我不知道如何解决。 I'm posting my code that I'm using for updating the data in a SQL Server table. 我正在发布用于更新SQL Server表中数据的代码。 Help me please. 请帮帮我。

using (var con = new SqlConnection(ConStr))
{
    string query = @"UPDATE tblCompanySetup SET companyName = @companyName";

    using (var cmd = new SqlCommand(query, con))
    {
        con.Open();
        cmd.Parameters.AddWithValue("@companyName", SqlDbType.NVarChar).Value = tbCompanyName.Text;
        cmd.ExecuteNonQuery();
    }
}

NOTE : I have previous value in textbox that I'm using for a new value to update. 注意 :我在文本框中具有用于更新新值的先前值。 It's working fine when I put that textbox empty and type new value. 当我将该文本框放空并键入新值时,它工作正常。 But when I select previous value in textbox and then input new value and click submit, it is actually saving the previous value I guess.. Also when I press the submit button, it refreshes the form and put the previous value again 但是,当我在文本框中选择上一个值,然后输入新值并单击提交时,它实际上是在保存我猜的上一个值。同样,当我按下“提交”按钮时,它会刷新表单并再次放入上一个值

It seems that you have retrieving method from a database on a page load. 看来您在页面加载时已从数据库检索方法。 Something like: 就像是:

protected void Page_Load(object sender, EventArgs e){
    RetrieveMyData(); // it is a method where you retrieve a data
}

Change this to: 将其更改为:

protected void Page_Load(object sender, EventArgs e){
    if (!IsPostBack)
        RetrieveMyData(); // it is a method where you retrieve a data
}

Your project works like: 您的项目如下:

1) Page_Load -> RetrieveMyData() 1)Page_Load-> RetrieveMyData()

2) Your textbox has some value from a database 2)您的文本框具有数据库中的某些价值

3) You changing that value to a new value 3)您将该值更改为新值

4) You click on a UPDATE button 4)单击UPDATE按钮

5) As you don't consider PostBack, it calls again RetrieveMyData and your textbox has the old value 5)由于您不考虑PostBack,它再次调用RetrieveMyData并且您的文本框具有旧值

6) Your Update method executes, but no changes occurs 6)您的Update方法执行,但未发生任何更改

If you will use PostBack as I showed: 如果您将使用我显示的PostBack:

1) Page_Load -> RetrieveMyData 1)Page_Load-> RetrieveMyData

2) Your textbox has some value from a database 2)您的文本框具有数据库中的某些价值

3) You change that value 3)你改变了那个价值

4) You click on a Update button 4)单击“更新”按钮

5) As you use !IsPostBack and button occur a postback, your RetrieveMyData method will not work here 5)正如你使用的那样!IsPostBack和按钮发生回发,你的RetrieveMyData方法在这里不起作用

6) Your update method will update the old value with a new one 6)您的更新方法将使用新值更新旧值

Hope it helps 希望能帮助到你

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

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