I couldn't get the contentEditable stuff to work the way I wanted so I need to switch it over so that the user types their stuff into textboxes and this is then saved into the DB. But I'm still getting the same error.
Your where clause is trying to update where the UserID = UserName. I would guess those are different datatypes. At least, they probably should be, right?
"Don't sweat the petty things, and don't pet the sweaty things"
- George Carlin
Yep, my bad. I just caught that. Check the where clause.
Also, while debugging you can hover over a value, supply a value, then hit "Enter" to assign it. So hover over the "Text" portion of h1Test.Text; and click on the
little popup to enter a value. If that works then your DB code is good, but there's something else in your app causing the problem
"Don't sweat the petty things, and don't pet the sweaty things"
- George Carlin
OK I'm getting confused. Originally you said h1Text.Text value was empty. Are you saying it does have a value, but it's just not updating that value in the DB? Are all the other fields getting updated?
I'd check the datatype length in your DB and make sure you have the correct length for newName
ex: @newName varchar(200)
"Don't sweat the petty things, and don't pet the sweaty things"
- George Carlin
NgJZliam.smi...
Member
37 Points
68 Posts
Update SQL Statement and Textbox Value
Apr 12, 2012 08:39 PM|LINK
I couldn't get the contentEditable stuff to work the way I wanted so I need to switch it over so that the user types their stuff into textboxes and this is then saved into the DB. But I'm still getting the same error.
This is my page code:
<header> <asp:Image ID="userImage" runat="server" AlternateText="User" CssClass="image"/> <h1><asp:Textbox ID="h1Test" runat="server" CssClass="intro"></asp:Textbox></h1> </header>And this is my code-behind. I don't get an error but when I F11 through the code, the h1Test.Text has no value against it and I don't know why.
protected void saveBtn_Click(object sender, EventArgs e) { string newName; string newIntro; string newEduc; string newWork; newName = h1Test.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); 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(); } }barryman9000
Participant
1698 Points
605 Posts
Re: Update SQL Statement and Textbox Value
Apr 12, 2012 08:59 PM|LINK
Your where clause is trying to update where the UserID = UserName. I would guess those are different datatypes. At least, they probably should be, right?
- George Carlin
NgJZliam.smi...
Member
37 Points
68 Posts
Re: Update SQL Statement and Textbox Value
Apr 12, 2012 09:00 PM|LINK
I have:
string username = HttpContext.Current.User.Identity.Name; myCommand.Parameters.AddWithValue("@userName", username.ToString());barryman9000
Participant
1698 Points
605 Posts
Re: Update SQL Statement and Textbox Value
Apr 12, 2012 09:03 PM|LINK
Yep, my bad. I just caught that. Check the where clause.
Also, while debugging you can hover over a value, supply a value, then hit "Enter" to assign it. So hover over the "Text" portion of h1Test.Text; and click on the little popup to enter a value. If that works then your DB code is good, but there's something else in your app causing the problem
- George Carlin
barryman9000
Participant
1698 Points
605 Posts
Re: Update SQL Statement and Textbox Value
Apr 12, 2012 09:05 PM|LINK
Also, can you verify that username has a value when you're debugging? That would also cause the sql to fail.
To check if the problem is in the SQL, wrap that whole thing in a try catch block. Then you can at least see what the exception is
try { string newName; string newIntro; string newEduc; string newWork; newName = h1Test.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); 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 (Exception ex) { string error = ex.Message; }- George Carlin
NgJZliam.smi...
Member
37 Points
68 Posts
Re: Update SQL Statement and Textbox Value
Apr 12, 2012 09:08 PM|LINK
Okay, I did that and it was successfully entered into the db.
When I hover over the h1Text.Text during the breakpoint it still contains the original text that was read from the db during page load.
barryman9000
Participant
1698 Points
605 Posts
Re: Update SQL Statement and Textbox Value
Apr 12, 2012 09:13 PM|LINK
OK I'm getting confused. Originally you said h1Text.Text value was empty. Are you saying it does have a value, but it's just not updating that value in the DB? Are all the other fields getting updated?
I'd check the datatype length in your DB and make sure you have the correct length for newName
ex: @newName varchar(200)
- George Carlin
NgJZliam.smi...
Member
37 Points
68 Posts
Re: Update SQL Statement and Textbox Value
Apr 12, 2012 09:17 PM|LINK
I thought it was going through as blank but it's not, it's just storing what was within the textbox initially.
So on page load h1.text currently contains the word "Hello".
If I change that to "Goodbye" and hit Save Changes, it is saving the original value (Hello).
The db is not the issue, everything is set to nvarchar(200)
barryman9000
Participant
1698 Points
605 Posts
Re: Update SQL Statement and Textbox Value
Apr 12, 2012 09:26 PM|LINK
Wow, that's bizare. That makes me think there something else in the markup causing that. Is that textbox within an UpdatePanel?
Try setting EnableViewState="False" on that textbox and see what happens.
- George Carlin
Evolutionz
Member
261 Points
95 Posts
Re: Update SQL Statement and Textbox Value
Apr 12, 2012 09:37 PM|LINK
well..you must be forgetting
if(!IsPostBack)
{
h1.Text="Hello";
}