on textChanged?

Last post 07-29-2009 1:53 PM by lolita8585. 18 replies.

Sort Posts:

  • on textChanged?

    06-19-2009, 11:50 AM
    • Member
      9 point Member
    • maisa
    • Member since 06-11-2009, 7:53 PM
    • Posts 23

     Hi,

    I'm using ASP.net c#, visual studio 2005, and access 2003. I'm trying to add a record to a database with the info of a textbox when that textbox changes. so I wrote this code in the Textchanged method

     protected void  _TextChanged(object sender, EventArgs e)
    {
        OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data      Source=C:\AccessDB\db1.mdb;User Id=admin;Password=;");
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandText = "insert into comments (comment) VALUES (?)";
        cmd.Parameters.AddWithValue("comment", COMMENT_BOX.Text.ToString());
        cmd.Connection = cn;
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
        cmd.Connection.Close();
        cmd.Dispose();
        cn.Dispose();
        COMMENT_BOX.Text = String.Empty;
    }

    The text box look like this:

     <asp:TextBox ID="COMMENT_BOX" Name="COMMENT_BOX" runat="server" Enabled="true" OnTextChanged="_TextChanged" Height="24px" Width="344px" /> Comments

    But this doesn't work, I even tryed to refresh the page after the text changes but no luck.. 

    Anything i'm missing here?

  • Re: on textChanged?

    06-19-2009, 12:15 PM
    Answer
    • All-Star
      26,188 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,732

     what do you mean by doesn't work? like in does not fire?

    use this AutoPostBack="true"  in here  :

    <asp:TextBox ID="COMMENT_BOX" Name="COMMENT_BOX" runat="server" Enabled="true" OnTextChanged="_TextChanged" Height="24px" Width="344px" AutoPostBack="true"/>

    also, you should keep you database code in the changed event under atry-catch block so that any database errors, if at all, will be reported

    try{}
    catch(Exception ex)
    {
        Response.Write(ex.Message);
    }

     

     

    Regards,
    Peter
  • Re: on textChanged?

    06-19-2009, 12:23 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Hi,

    I the text box set AutoPostBack="true"

    Thanks

    Ratheesh

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: on textChanged?

    06-19-2009, 12:28 PM
    • Star
      14,074 point Star
    • karan@dotnet
    • Member since 09-13-2007, 8:15 PM
    • New York
    • Posts 2,441

     AutoPostback = True is missing from your textbox.

    here:

     

    <asp:TextBox ID="COMMENT_BOX" Name="COMMENT_BOX" runat="server" Enabled="true" OnTextChanged="_TextChanged" AutoPostBack="true" Height="24px" Width="344px" />
      
    Thanks,
    Karan

    ~ Remember To Mark The Post(s) That Helped You As The ANSWER ~
  • Re: on textChanged?

    06-19-2009, 12:51 PM
    • Member
      9 point Member
    • maisa
    • Member since 06-11-2009, 7:53 PM
    • Posts 23

    Still no results and i received a syntax error when I added AutoPostBack="true"

     <asp:TextBox ID="COMMENT_BOX" Name="COMMENT_BOX" runat="server" Enabled="true" OnTextChanged="_TextChanged" Height="24px" Width="344px" AutoPostBack="true"/>

  • Re: on textChanged?

    06-19-2009, 12:52 PM
    • All-Star
      26,188 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,732

     type in some text and 'tab' away from the textbox or click, say, another button...

    Regards,
    Peter
  • Re: on textChanged?

    06-19-2009, 12:55 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Hi,

    Please post here the error you are getting..

    Thanks

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: on textChanged?

    06-19-2009, 1:10 PM
    • Member
      9 point Member
    • maisa
    • Member since 06-11-2009, 7:53 PM
    • Posts 23
    I tried that, still doesn't work, and I don't know why I'm getting the syntax error
  • Re: on textChanged?

    06-19-2009, 1:21 PM
    • All-Star
      26,188 point All-Star
    • PeteNet
    • Member since 01-21-2009, 1:15 PM
    • Posts 3,732

    ....if all you're doing is saving text into the database then you would rather simply keep a button there and fire its click event and save...so that most of your code is already working.

    ...textchanged works for me though.....remember textchanged might cause multiple postbacks every time the text changes and normally one would try to avoid re-loading a complete page all the time or saving information into the database repeatedly like this.

    Regards,
    Peter
  • Re: on textChanged?

    06-19-2009, 1:28 PM
    • Member
      9 point Member
    • maisa
    • Member since 06-11-2009, 7:53 PM
    • Posts 23

     Well.. I don't have a choice here.. My boss wants me to save the information when it is written, without hitting an add button.. so I gotta do this

  • Re: on textChanged?

    06-19-2009, 1:34 PM

    set  AutoPostBack="True" for your textbox. and try to clear your textbox in post back

    page_load()

    if  not ispostback then

    else

    better clear textbox  here

    endif

    Remember to click “Mark as Answer” on the post, if it helps you. Because It helps others to find the solution.

    Srinivas Kotra.


  • Re: on textChanged?

    06-19-2009, 1:39 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 6:05 PM
    • Posts 1,198

    Could u post your .aspx and CS code here..?

    Thanks

    Ratheesh

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: on textChanged?

    06-19-2009, 1:44 PM
    • Star
      14,074 point Star
    • karan@dotnet
    • Member since 09-13-2007, 8:15 PM
    • New York
    • Posts 2,441

     this is fine just copy and past this:

    <asp:TextBox ID="COMMENT_BOX" Name="COMMENT_BOX" runat="server" Enabled="true" OnTextChanged="_TextChanged" Height="24px" Width="344px" AutoPostBack="true" /> 

    Also the textchange event will work only when you tab out, so say you are writing something in your textbox and your press tab then your textchanged event would hit

    So in your case you should look at some async saving methods to save into your db

     

    Thanks,
    Karan

    ~ Remember To Mark The Post(s) That Helped You As The ANSWER ~
  • Re: on textChanged?

    06-19-2009, 1:48 PM
    • Member
      9 point Member
    • maisa
    • Member since 06-11-2009, 7:53 PM
    • Posts 23

     I tried that already and it didn't work and i also recieved a syntax error...

  • Re: on textChanged?

    06-19-2009, 2:04 PM
    Answer
    • Star
      14,074 point Star
    • karan@dotnet
    • Member since 09-13-2007, 8:15 PM
    • New York
    • Posts 2,441

    I tested that line and it does not throws any syntax errors. please check may be there are some other errors on your page

    Thanks,
    Karan

    ~ Remember To Mark The Post(s) That Helped You As The ANSWER ~
Page 1 of 2 (19 items) 1 2 Next >