A few things...on your page load, you have a
catch
that simply discards any SQL exception. You should almost never do that. In general, you should either (at least) log or mitigate the exception.
In your button click, you save the exception in some status text. Does an exception occur? Could you please share the text if it does?
Some additional observation, you have resource leaks. You don't dispose of
cmd
at all. You don't dispose of
sd
. And, you don't dispose of
cmd2
, if an exception occurs.
I suggest you familiarize yourself with the C#
using
statement. A typical pattern would be:
using(var con = new SqlConnection(conStr))
{
con.Open();
using(var cmd = new SqlCommand(cmdStr, con))
{
}
}
With the
using
statements, there is far less chance you'll accidentally introduce a resource leak.
Adding one other point, you should use SQL parameters instead of string concatenation when building your commands. Otherwise, you are a very easy target for hackers via a SQL injection attack.
See:
SQL Injection | Microsoft Docs[
^]
Finally, without any exception text, I cannot say definitively what your problem might be. However, I can say that your SQL update command is not valid.
At a minimum, you are missing quoting on the value of
StudentClass
column and you are missing a space before the WHERE command.