[Inserting data from form]How to change value of TextBox before inserting it into database by FormParameter?

Last post 07-04-2007 5:14 AM by szmitek. 3 replies.

Sort Posts:

  • Hmm [^o)] [Inserting data from form]How to change value of TextBox before inserting it into database by FormParameter?

    07-03-2007, 3:45 PM
    • Loading...
    • szmitek
    • Joined on 03-03-2007, 7:22 PM
    • Chotomów, Poland
    • Posts 61

     How to change value of TextBox before inserting it into database by FormParameter?

    Here is test script. I would like to create script which add "ok" to value of TextBox with ID="name_pl" and then insert it to database using FromParameter. For example, when I write "let" in TextBox and click "Submit", script insert to database "letok". How to do this? I have tried to do this by adding name_pl = name_pl + "ok"; before links.Insert(); but this cause error "CS0029: Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.TextBox'".

     Could you help me? Test script:

    <%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head><title>Untitled Page</title><link href="../format.css" rel="stylesheet" type="text/css" />

    <script type="text/c#" runat="server">

    public void submit_click(object source, EventArgs e)

    {

    name_pl = name_pl +
    "ok";

    links.Insert();

    }

    </script>

    </head>

    <body>

     

    <div>

    <form id="form" runat="server" action="Request.ServerVariables['SCRIPT_NAME'];" defaultbutton="submit">

     

    <p>

    <strong>Text:</strong>

    <asp:TextBox ID="name_pl" runat="server" AutoPostBack="true" Width="300px"></asp:TextBox>

    </p>

     

    &nbsp;

    <p><asp:Button ID="submit" runat="server" OnClick="submit_click" Text="Submit" /></p>

    </form>

     

    <asp:AccessDataSource ID="links" DataFile="~/App_Data/data.mdb" runat="server" InsertCommand="INSERT Into links (name_pl) Values (@name_pl)">

    <InsertParameters>

    <asp:FormParameter FormField="name_pl" Name="name_pl" />

    </InsertParameters>

    </asp:AccessDataSource>

    </div>

     

    </body>

    </html>

    I am asking for help politely.

  • Re: [Inserting data from form]How to change value of TextBox before inserting it into database by FormParameter?

    07-03-2007, 4:45 PM
    • Loading...
    • leoalves
    • Joined on 06-19-2007, 12:29 PM
    • Posts 96

    You have to add this object in you insert parameters and then in the event inserting of the dataset (that is executed before the insert command) you can set the object you want to the value you want.

     

  • Re: [Inserting data from form]How to change value of TextBox before inserting it into database by FormParameter?

    07-03-2007, 5:19 PM
    Answer
    • Loading...
    • farazsk11
    • Joined on 02-22-2006, 8:10 PM
    • Sharjah, UAE
    • Posts 1,109

    Hi,

    Try to write your statement name_pl = name_pl + "ok";  as name_pl.Text = name_pl.Text + "ok";  because you are trying to append some string with the text of name_pl not with the control object.

    Hope it will help you out.

    Thanks and best regards,

    Faraz Shah Khan
    MCP, MCAD.Net, MCSD.Net, MCTS-Win/Web, MCPD-Web
    Blog
  • Thank you. One need to change FormParameter into ControlParameter

    07-04-2007, 5:14 AM
    • Loading...
    • szmitek
    • Joined on 03-03-2007, 7:22 PM
    • Chotomów, Poland
    • Posts 61

    It run after adding your code and changing FormParameter into ControlParameter.

    <%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head><title>Untitled Page</title><link href="../format.css" rel="stylesheet" type="text/css" />

    <script type="text/c#" runat="server">

    public void submit_click(object source, EventArgs e)

    {

    name_pl.Text = name_pl.Text + "ok";

    links.Insert();

    }

    </script>

    </head>

    <body>

     

    <div>

    <form id="form" runat="server" action="Request.ServerVariables['SCRIPT_NAME'];" defaultbutton="submit">

     

    <p>

    <strong>Text:</strong>

    <asp:TextBox ID="name_pl" runat="server" AutoPostBack="true" Width="300px"></asp:TextBox>

    </p>

     

    &nbsp;

    <p><asp:Button ID="submit" runat="server" OnClick="submit_click" Text="Dodaj" /></p>

    </form>

     

    <asp:AccessDataSource ID="links" DataFile="~/App_Data/data.mdb" runat="server" InsertCommand="INSERT Into links (name_pl) Values (@text)"><InsertParameters><asp:ControlParameter ControlID="name_pl" PropertyName="Text" Name="text"/></InsertParameters>

     

    </asp:AccessDataSource>

    </div>

     

    </body>

    </html>

    Thank you!

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter