Solved

Getting old values of a textbox

Posted on 2009-04-12
12
855 Views
Last Modified: 2012-05-06
I want to get new values from the Text box when i click the save button and then i want to update the database whit the new values of the Text box....I have looked for some solutions but cant figure it out...Here is the code i am using...
<table style="width:100%;">
        <tr>
            <td>
                <asp:Label ID="lblEditPost" runat="server" Text="Edit post:"></asp:Label>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                <asp:TextBox ID="txtEditPost" runat="server" Height="202px" 
                    TextMode="MultiLine" Width="501px" EnableViewState="true"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnUpdatePost" runat="server" Text="Save" />
                <br /><br />
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="txtEditPost" ErrorMessage="The post cant be empty!"></asp:RequiredFieldValidator>
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
------------------------------------------------------------------
Imports System.Web.Security
Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration
Partial Class editPost
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        getPost()
    End Sub
    Protected Sub btnUpdatePost_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdatePost.Click
 
        updatePost(txtEditPost.Text)
 
    End Sub
 
    Sub updatePost(ByVal strPostContent As String)
 
        Dim id As Integer = Request.QueryString("id")
 
        Dim MyConn As OleDbConnection = New OleDbConnection(ConfigurationManager.AppSettings("strConn"))
        Dim MySQL As String = "UPDATE [tblPostings] SET [PostContent]=@PostContent WHERE [tblPostings.PostID] = @id"
        Dim MyCmd As New OleDbCommand(MySQL, MyConn)
 
        With MyCmd.Parameters
            .Add(New OleDbParameter("@id", id))
            .Add(New OleDbParameter("@PostContent", strPostContent))
        End With
 
        MyConn.Open()
        MyCmd.ExecuteNonQuery()
        'Response.Redirect("viewtopic.aspx?id=", id)
        MyConn.Close()
 
    End Sub
End Class

Open in new window

0
Comment
Question by:Neste
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
12 Comments
 
LVL 10

Expert Comment

by:Espavo
ID: 24125160
This looks like it should work...
Are you passing a value for Request.QueryString("id")?
0
 
LVL 39

Accepted Solution

by:
abel earned 250 total points
ID: 24125215
In the code you are showing, a few things bear a little attention:

> Dim id As Integer = Request.QueryString("id")

there's no sign of the "id" in your querystring. Since you use a POST method, maybe you meant to get the contents of a hidden field that goes by the name "id"

Same line: QueryString returns a string. You are requesting an integer. You should get an error there. Either way, it is best to convert the string to an integer before assigning it to one.

> getPost()

this method is called in the Page_Load, which also means it will be called before the event of the button_click. You do not show that method, but if it sets any values, i.e. of your txtEditPost, it will reset its value to its original one, which is then the cause of your problem. Can you show us that method too, please?

-- Abel --
0
 

Author Comment

by:Neste
ID: 24125230
Yeah, iam passing the PostID...

Really cant figure whats the problem is, why its not saving the new value to txtEditPost.Text...Everytime i try and edit any posts and click the Save button i just get the same old value :S
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Neste
ID: 24125250
i dont have any other methods for it...i didn't use getPost()

I have a gridview in viewtopic.aspx whit user posts...When i click a specific post i send the PostID to editpost.aspx....

Almost everything works except that when i edit a post i allways and hit the Save button, it posts back the old value of the post...giving txtEditPost.Text the old value...
0
 

Author Comment

by:Neste
ID: 24125264
Here is the getPost() this code is ok i think...sorry didn't get you, so tired hehe
    Sub getPost()
 
        Dim id As Integer = Request.QueryString("id")
 
        Dim MyConn As OleDbConnection = New OleDbConnection(ConfigurationManager.AppSettings("strConn"))
        Dim MySQL As String = "SELECT * FROM [tblPostings] WHERE [tblPostings.PostID] = @id"
        Dim MyCmd As New OleDbCommand(MySQL, MyConn)
 
        With MyCmd.Parameters
            .Add(New OleDbParameter("@id", id))
        End With
 
        MyConn.Open()
        Dim dataReader As OleDbDataReader = MyCmd.ExecuteReader()
 
        Dim PostContent As String = dataReader.Read()
 
        Dim post As String = dataReader("PostContent")
 
        txtEditPost.Text = post
 
        txtEditPost.DataBind()
 
        MyConn.Close()
 
    End Sub

Open in new window

0
 

Author Comment

by:Neste
ID: 24125281
hmmm now i know what you mean abel...Can i do something about this...call the getpost() somewhere else ??
0
 

Author Comment

by:Neste
ID: 24125316
Got it, now it works fine...

        If Not IsPostBack Then
            getPost()
        End If

Thx Abel
0
 
LVL 39

Expert Comment

by:abel
ID: 24125363
You were quicker than me! Yes, the Page.IsPostBack is what you should use to differentiate between the first access to a page or any time an action is requested.
0
 

Author Comment

by:Neste
ID: 24125584
Damn now i got another problem whit the same code...

Now i get the new value from txtEditPost.Text and everything works great BUT it dont update the DB :S

I am not getting any error's but i see in db that nothing is updated...

Any suggestions what problem may be?

0
 

Author Comment

by:Neste
ID: 24125617
hmmm MyCmd.ExecuteNonQuery() returns 0

Any help ? :)
0
 
LVL 39

Expert Comment

by:abel
ID: 24125735
Though that is a completely new question and there may be experts that are better at that subject than I, I can give you a hint of where to look. The ExecuteNonQuery returns the number of rows affected. If it fails, it will raise an error. That it returns zero means that the WHERE-part of the UPDATE query does not select anything.

Try to place a breakpoint in your code and create the statement by hand from this:

        UPDATE [tblPostings] SET [PostContent]=@PostContent WHERE [tblPostings.PostID] = @id

by replacing @id and @PostContent with the values they are holding (you can see that on the mouseover while in break mode). Go to your database (use any SQL client you like) and execute the statement by hand and tweak uit until it works.

Note that the WHERE-part can be used in a SELECT statement to find out whether you are actually selecting anything to be updated (the most common error is that you are not selecting anything):

       SELECT * FROM  tblPostings WHERE tblPostings.PostID = @id

-- Abel --
 
0
 

Author Comment

by:Neste
ID: 24127924
My parameters were wrong, thx abel :)
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net mvc , views, hidden values ? 2 26
Google Maps with Webforms 1 45
Record locking on classic ASP 3 64
async questions 5 48
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question