Using html5 contentEditable to save to dbhttp://forums.asp.net/t/1792205.aspx/1?Using+html5+contentEditable+to+save+to+dbThu, 12 Apr 2012 21:38:33 -040017922054929343http://forums.asp.net/p/1792205/4929343.aspx/1?Using+html5+contentEditable+to+save+to+dbUsing html5 contentEditable to save to db <p>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.</p> <p>This is my code-behind:</p> <p></p> <pre class="prettyprint">string newName; string newIntro; string newEduc; string newWork; newName = h1.Text; newIntro = intro.Text; newEduc = educ.Text; newWork = employ.Text; string connectionInfo = ConfigurationManager.ConnectionStrings[&quot;ApplicationServices&quot;].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionInfo)) { connection.Open(); SqlCommand myCommand = new SqlCommand(&quot;UPDATE simpleContent SET userName = @newName, infoContent = @newIntro, educContent = @newEduc, workContent = @newWork WHERE userID = @userName&quot;, connection); try { string username = HttpContext.Current.User.Identity.Name; myCommand.Parameters.AddWithValue(&quot;@userName&quot;, username.ToString()); myCommand.Parameters.AddWithValue(&quot;@newName&quot;, newName.ToString()); myCommand.Parameters.AddWithValue(&quot;@newIntro&quot;, newIntro.ToString()); myCommand.Parameters.AddWithValue(&quot;@newEduc&quot;, newEduc.ToString()); myCommand.Parameters.AddWithValue(&quot;@newWork&quot;, newWork.ToString()); myCommand.ExecuteNonQuery(); connection.Close(); } catch { Response.Redirect(&quot;http://www.google.co.uk&quot;); } } }</pre> <p>The code does work as the try part of the statement is running but the changes aren't being saved to the db. &nbsp;Any ideas as to why?</p> <p>Here is a snippet of the code I'm using on the client side:</p> <pre class="prettyprint">&lt;div id="container"&gt; &lt;header&gt; &lt;asp:Image ID="userImage" runat="server" AlternateText="User" CssClass="image"/&gt; &lt;h1&gt;&lt;asp:Label ID="h1" runat="server" CssClass="intro" contenteditable="true"&gt;&lt;/asp:Label&gt;&lt;/h1&gt; &lt;/header&gt;</pre> <p><br> <br> </p> <p></p> <p></p> 2012-04-12T14:15:23-04:004929475http://forums.asp.net/p/1792205/4929475.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>I would try putting a breakpoint at the beginning of this code behind and F11 through it to see where its bombing out.&nbsp; 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:</p> <pre class="prettyprint">string newName; string newIntro; string newEduc; string newWork; newName = h1.Text; newIntro = intro.Text; newEduc = educ.Text; newWork = employ.Text; string connectionInfo = ConfigurationManager.ConnectionStrings[&quot;ApplicationServices&quot;].ConnectionString; using (SqlConnection connection = new SqlConnection(connectionInfo)) { connection.Open(); SqlCommand myCommand = new SqlCommand(&quot;UPDATE simpleContent SET userName = @newName, infoContent = @newIntro, educContent = @newEduc, workContent = @newWork WHERE userID = @userName&quot;, connection); try { string username = HttpContext.Current.User.Identity.Name; myCommand.Parameters.AddWithValue(&quot;@userName&quot;, username.ToString()); myCommand.Parameters.AddWithValue(&quot;@newName&quot;, newName.ToString()); myCommand.Parameters.AddWithValue(&quot;@newIntro&quot;, newIntro.ToString()); myCommand.Parameters.AddWithValue(&quot;@newEduc&quot;, newEduc.ToString()); myCommand.Parameters.AddWithValue(&quot;@newWork&quot;, newWork.ToString()); myCommand.ExecuteNonQuery(); } catch { Response.Redirect(&quot;http://www.google.co.uk&quot;); } connection.Close(); } }</pre> <p></p> 2012-04-12T15:25:48-04:004929490http://forums.asp.net/p/1792205/4929490.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>Already tried moving the connection.close();</p> <p>The only thing I can think of is that its not reading the text that is being written in the labels .</p> 2012-04-12T15:30:18-04:004929495http://forums.asp.net/p/1792205/4929495.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>Then F11 through it one step at a time and watch the Autos panel. That will show you what is inside your variables.</p> 2012-04-12T15:34:41-04:004929497http://forums.asp.net/p/1792205/4929497.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>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.</p> 2012-04-12T15:35:50-04:004929500http://forums.asp.net/p/1792205/4929500.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>Ah, ok I saw it, I woudl still try the F11 thing just to see what the values of your variables are.</p> 2012-04-12T15:36:32-04:004929509http://forums.asp.net/p/1792205/4929509.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>F11?</p> <p>I'm not sure what you mean. &nbsp;I hit F11 and it builds my project.</p> 2012-04-12T15:40:22-04:004929523http://forums.asp.net/p/1792205/4929523.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>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.</p> 2012-04-12T15:50:31-04:004929531http://forums.asp.net/p/1792205/4929531.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>I've set a breakpoint at the start of the btnSave_Click function, hit F5 and then hit F11 but nothing happens</p> 2012-04-12T15:53:36-04:004929534http://forums.asp.net/p/1792205/4929534.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>Yeah, it is reading the original values and not what I've changed it to.</p> <p>I'm not sure as to why it is reading the original value and not what has been typed in in it's place.</p> 2012-04-12T15:55:15-04:004929613http://forums.asp.net/p/1792205/4929613.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>I think the issue is that I am adding contentEditable=&quot;true&quot; to an ASP Label which you can't really do.</p> <p>I've tried doing this:</p> <p>&lt;h1 id=&quot;h1Test&quot; contenteditable=&quot;true&quot; runat=&quot;server&quot;&gt;&lt;asp:Label ID=&quot;h1&quot; runat=&quot;server&quot; CssClass=&quot;intro&quot;&gt;&lt;/asp:Label&gt;&lt;/h1&gt;</p> <p>But I'm not sure how to get the h1Test assigned to a string variable in the code-behind file</p> 2012-04-12T16:36:06-04:004929630http://forums.asp.net/p/1792205/4929630.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>A label is not an editable control. use a text box.</p> 2012-04-12T16:45:11-04:004929638http://forums.asp.net/p/1792205/4929638.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>contentEditable isn't &quot;supported&quot; by an ASP control. &nbsp;Which is why I think it isn't reading the changed value.</p> <p>I think it has to be html controls but I'm not sure how I would use them in the code-behind.</p> 2012-04-12T16:50:29-04:004929670http://forums.asp.net/p/1792205/4929670.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>Are you trying to change these database values on the fly? If so, you're going to probably need to do an OnTextChanged event for each field. Others here may have other suggestions but without seeing your entire project, I can only fathom a guess.</p> 2012-04-12T17:07:23-04:004929828http://forums.asp.net/p/1792205/4929828.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>Okay, this is what I have on my page entitled EditMyContent.aspx</p> <pre class="prettyprint">&lt;p&gt; &lt;asp:Button ID=&quot;saveBtn&quot; runat=&quot;server&quot; Text=&quot;Save Changes&quot; onclick=&quot;saveBtn_Click&quot; /&gt; &lt;/p&gt; &lt;/header&gt; &lt;div id=&quot;container&quot;&gt; &lt;header&gt; &lt;asp:Image ID=&quot;userImage&quot; runat=&quot;server&quot; AlternateText=&quot;User&quot; CssClass=&quot;image&quot;/&gt; &lt;h1&gt;&lt;asp:Label ID=&quot;h1&quot; runat=&quot;server&quot; CssClass=&quot;intro&quot; contentEditable=&quot;true&quot;&gt;&lt;/asp:Label&gt;&lt;/h1&gt; &lt;/header&gt;</pre> <p>And this is everything I have in my saveBtn_Click function in the code-behind</p> <pre class="prettyprint">protected void saveBtn_Click(object sender, EventArgs e) { 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(); } }</pre> <p><br> <br> <br> </p> 2012-04-12T18:54:02-04:004930037http://forums.asp.net/p/1792205/4930037.aspx/1?Re+Using+html5+contentEditable+to+save+to+dbRe: Using html5 contentEditable to save to db <p>But label is not editable. You have to use a textbox or something like that. Try textbox without that contentEditable tag since textbox is editable by default.</p> 2012-04-12T21:38:33-04:00