Yeah this is a pretty simple topic, but my form just isn’t updating the row in the database:
Here’s my code:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If Request.QueryString("a") = "edit" Then
Dim CategoryText As String = lstCategory.SelectedItem.Text
Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=LogansArchive; Integrated Security=True;")
Dim cmd As New SqlCommand("UpdateArticle", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@ArticleID", Request.QueryString("i"))
cmd.Parameters.AddWithValue("@Category", CategoryText)
cmd.Parameters.AddWithValue("@ArticleTitle", txtNewTitle.Text)
cmd.Parameters.AddWithValue("@ArticleContent", txtContent.Text)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
Else
Dim CategoryText As String = lstCategory.SelectedItem.Text
Dim conn As New SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=LogansArchive; Integrated Security=True;")
Dim cmd As New SqlCommand("AddArticle", conn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@Category", CategoryText)
cmd.Parameters.AddWithValue("@Date", Now)
cmd.Parameters.AddWithValue("@ArticleTitle", txtNewTitle.Text)
cmd.Parameters.AddWithValue("@ArticleContent", txtContent.Text)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End If
End Sub
Here’s the SP that the code refers to:
ALTER PROCEDURE [dbo].[UpdateArticle]
-- Add the parameters for the stored procedure here
@ArticleID int,
@Category varchar(20),
@ArticleTitle varchar(100),
@ArticleContent text
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
UPDATE Articles
SET
Articles.Category = (SELECT CategoryID FROM Categories WHERE CategoryName = @Category),
Articles.ArticleTitle = @ArticleTitle,
Articles.ArticleContent = @ArticleContent
WHERE Articles.ArticleID = @ArticleID
END
I’ve never used nested SELECTs in SQL before so I’m not sure if I did it right.
Aside from that, this is all stuff I’ve done before so I’m relatively sure that it’s all correct.
Finally, there are no errors being reported when I debug, although the updates don’t seem to be coming through from the text fields (txtContent.Text isn’t changing to reflect the new value).
Thanks in advance for your assistance.
EDIT 1:
Thanks for the updates, I’ve looked at the CInt(Request.Querystring("i")) issue, and it doesn’t solve the problem. I set a breakpoint on the start of the procedure and hovered over txtContent.Text to check the value of it’s Text property in the intellisense. Bear in mind that the debugger is still on the first line of the procedure. The txtContent.Text property as shown by the debugger at this point contains none of the changes I made while testing.
Well it’s taken 3 months, but I just found the answer.
The reason the debugger doesn’t see any changes to my text fields is … wait for it…
The fields are being populated from the database on page load.
I just had the same problem at work, and one of the other developers hit me with this. To fix the problem, place the code that populates the form into the following
IFstatement: