Last post Jan 22, 2018 11:22 AM by jimmy69
Jan 21, 2018 11:06 AM|UltraJ|LINK
Hi. I can't get an ASP.NET webform to update a database. I'm trying to edit an existing record in the database. The webform populates the data from the record into the form. The user then changes data and updates the record in the database when the form
The problem is that nothing is changed in the database when a modified form is submitted. What am I doing wrong here? The SQL works in MSSQL Management Studio.
private void SaveToDatabase ()
using (SqlConnection conn = new SqlConnection (_connectionString_Bluebook))
string sql = @"update Companies
set CompanyName=@CompanyName, AccountNo=@AccountNo
where AccountNo=" + _accountNo;
using (SqlCommand command = new SqlCommand (sql, conn))
command.Parameters.Add (new SqlParameter ("@CompanyName", TextBox_CompanyName.Text));
command.Parameters.Add (new SqlParameter ("@AccountNo", TextBox_Account.Text));
Jan 21, 2018 12:48 PM|Mixfederico|LINK
Jan 21, 2018 01:13 PM|DA924|LINK
Well, stop execution with the debugger and use QuickWatch
You can use QW on the command.ExecuteNonQuery
(); on the 'command' object to see the T-SQL that is going to be executed. You can copy the T-SQL out of the 'command' object and execute it in SSMS to see what it actually going to do.
Jan 22, 2018 09:56 AM|jimmy69|LINK
@ultraJ => i've just copy paste your code into a test solution i have
change the connection string to my database, modify just the name of the table in your command update but keept the parameter name (@companyName, @accountNo) ..
the result is my record is well updated ...please check the value inside => _accountNo
If no record aren't update perhpas the id isn't correct ..
Jan 22, 2018 11:11 AM|X.Daisy|LINK
1) Add a breakpoint for SaveToDatabase to check if the method is called correctly when form is submitted.
2) I can't find where you define '_accountNo'. Please make sure this value is valid.(You can also use breakpoint to check its value)
Jan 22, 2018 11:22 AM|jimmy69|LINK
another info i think who is intessant you can use the sql server profiler for to see what's you send to the back end..
i used it and it's very nice tool