I would try putting a breakpoint at the beginning of this code behind and F11 through it to see where its bombing out. One thing I can suggest is move your connection.Close(); to after the catch block since that will leave the connection open. So your code
will look more like this:
Wait, you're referring to username but where is the variable declaration? If you try inserting a null variable your database is probably going to pitch a fit.
NgJZliam.smi...
Member
37 Points
68 Posts
Using html5 contentEditable to save to db
Apr 12, 2012 02:15 PM|LINK
I'm trying to allow users to edit the content of their webpage and then hit save so that the changes are then saved into a db.
This is my code-behind:
string newName; string newIntro; string newEduc; string newWork; newName = h1.Text; newIntro = intro.Text; newEduc = educ.Text; newWork = employ.Text; string connectionInfo = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionInfo)) { connection.Open(); SqlCommand myCommand = new SqlCommand("UPDATE simpleContent SET userName = @newName, infoContent = @newIntro, educContent = @newEduc, workContent = @newWork WHERE userID = @userName", connection); try { string username = HttpContext.Current.User.Identity.Name; myCommand.Parameters.AddWithValue("@userName", username.ToString()); myCommand.Parameters.AddWithValue("@newName", newName.ToString()); myCommand.Parameters.AddWithValue("@newIntro", newIntro.ToString()); myCommand.Parameters.AddWithValue("@newEduc", newEduc.ToString()); myCommand.Parameters.AddWithValue("@newWork", newWork.ToString()); myCommand.ExecuteNonQuery(); connection.Close(); } catch { Response.Redirect("http://www.google.co.uk"); } } }The code does work as the try part of the statement is running but the changes aren't being saved to the db. Any ideas as to why?
Here is a snippet of the code I'm using on the client side:
<div id="container"> <header> <asp:Image ID="userImage" runat="server" AlternateText="User" CssClass="image"/> <h1><asp:Label ID="h1" runat="server" CssClass="intro" contenteditable="true"></asp:Label></h1> </header>bbcompent1
All-Star
32982 Points
8508 Posts
Moderator
Re: Using html5 contentEditable to save to db
Apr 12, 2012 03:25 PM|LINK
I would try putting a breakpoint at the beginning of this code behind and F11 through it to see where its bombing out. One thing I can suggest is move your connection.Close(); to after the catch block since that will leave the connection open. So your code will look more like this:
string newName; string newIntro; string newEduc; string newWork; newName = h1.Text; newIntro = intro.Text; newEduc = educ.Text; newWork = employ.Text; string connectionInfo = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionInfo)) { connection.Open(); SqlCommand myCommand = new SqlCommand("UPDATE simpleContent SET userName = @newName, infoContent = @newIntro, educContent = @newEduc, workContent = @newWork WHERE userID = @userName", connection); try { string username = HttpContext.Current.User.Identity.Name; myCommand.Parameters.AddWithValue("@userName", username.ToString()); myCommand.Parameters.AddWithValue("@newName", newName.ToString()); myCommand.Parameters.AddWithValue("@newIntro", newIntro.ToString()); myCommand.Parameters.AddWithValue("@newEduc", newEduc.ToString()); myCommand.Parameters.AddWithValue("@newWork", newWork.ToString()); myCommand.ExecuteNonQuery(); } catch { Response.Redirect("http://www.google.co.uk"); } connection.Close(); } }NgJZliam.smi...
Member
37 Points
68 Posts
Re: Using html5 contentEditable to save to db
Apr 12, 2012 03:30 PM|LINK
Already tried moving the connection.close();
The only thing I can think of is that its not reading the text that is being written in the labels .
bbcompent1
All-Star
32982 Points
8508 Posts
Moderator
Re: Using html5 contentEditable to save to db
Apr 12, 2012 03:34 PM|LINK
Then F11 through it one step at a time and watch the Autos panel. That will show you what is inside your variables.
bbcompent1
All-Star
32982 Points
8508 Posts
Moderator
Re: Using html5 contentEditable to save to db
Apr 12, 2012 03:35 PM|LINK
Wait, you're referring to username but where is the variable declaration? If you try inserting a null variable your database is probably going to pitch a fit.
bbcompent1
All-Star
32982 Points
8508 Posts
Moderator
Re: Using html5 contentEditable to save to db
Apr 12, 2012 03:36 PM|LINK
Ah, ok I saw it, I woudl still try the F11 thing just to see what the values of your variables are.
NgJZliam.smi...
Member
37 Points
68 Posts
Re: Using html5 contentEditable to save to db
Apr 12, 2012 03:40 PM|LINK
F11?
I'm not sure what you mean. I hit F11 and it builds my project.
bbcompent1
All-Star
32982 Points
8508 Posts
Moderator
Re: Using html5 contentEditable to save to db
Apr 12, 2012 03:50 PM|LINK
Sorry, I meant set a breakpoint at hte beginning of the routine. Hit F5 for debug, then F11 to step through one line at a time.
NgJZliam.smi...
Member
37 Points
68 Posts
Re: Using html5 contentEditable to save to db
Apr 12, 2012 03:53 PM|LINK
I've set a breakpoint at the start of the btnSave_Click function, hit F5 and then hit F11 but nothing happens
NgJZliam.smi...
Member
37 Points
68 Posts
Re: Using html5 contentEditable to save to db
Apr 12, 2012 03:55 PM|LINK
Yeah, it is reading the original values and not what I've changed it to.
I'm not sure as to why it is reading the original value and not what has been typed in in it's place.