newbie: inserting data into sql form writes only null values

Discussion in 'ASP .Net' started by thersitz, Jan 31, 2007.

  1. thersitz

    thersitz Guest

    Hi,

    using VStudio 2005/sql server 2005

    Have a simple web form that inserts the results in a table. It seems to
    write to the table, but not the values from the asp forms fields. It adds a
    new record and increments the id field by one -- but in all the other
    fields, it merely writes Null to the fields. He are some other points:

    I am not inserting data into every field -- for test purposes I am only
    using 4 fields.
    The id field is NOT one of the fields on the asp form -- although it is the
    only field that actually writes a correct value

    here's the code:
    thersitz, Jan 31, 2007
    #1
    1. Advertising

  2. thersitz

    thersitz Guest

    using VStudio 2005/sql server 2005

    Have a simple web form that inserts the results in a table. It seems to
    write to the table, but not the values from the asp forms fields. It adds a
    new record and increments the id field by one -- but in all the other
    fields, it merely writes Null to the fields. He are some other points:

    I am not inserting data into every field -- for test purposes I am only
    using 4 fields.
    The id field is NOT one of the fields on the asp form -- although it is the
    only field that actually writes a correct value

    here's the code:

    <asp:TextBox ID="FirstName" runat="server"></asp:TextBox><br />
    Last name:
    <asp:TextBox ID="LastName" runat="server"></asp:TextBox><br />
    Address:
    <asp:TextBox ID="Address" runat="server"></asp:TextBox><br />
    City:
    <asp:TextBox ID="City" runat="server"></asp:TextBox><br />
    Year created:
    <asp:DropDownList ID="YearCreated" runat="server">
    </asp:DropDownList><br />

    <asp:Button ID="Save" runat="server" Text="Save" />
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString=
    "<%$ ConnectionStrings:WHCConnectionString %>"
    InsertCommand=
    "INSERT INTO
    [artsfestival] ([lastName], [firstName], [address], [city])
    VALUES
    (@lastName, @firstName, @address, @city)">
    <InsertParameters>
    <asp:FormParameter Name="lastName" Type="String"
    FormField="LastName" />
    <asp:FormParameter Name="firstName" Type="String"
    FormField="FirstName" />
    <asp:FormParameter Name="address" Type="String"
    FormField="Address" />
    <asp:FormParameter Name="city" Type="String"
    FormField="City" />
    </InsertParameters>
    </asp:SqlDataSource>




    "thersitz" <> wrote in message
    news:...
    > Hi,
    >
    > using VStudio 2005/sql server 2005
    >
    > Have a simple web form that inserts the results in a table. It seems to
    > write to the table, but not the values from the asp forms fields. It adds
    > a new record and increments the id field by one -- but in all the other
    > fields, it merely writes Null to the fields. He are some other points:
    >
    > I am not inserting data into every field -- for test purposes I am only
    > using 4 fields.
    > The id field is NOT one of the fields on the asp form -- although it is
    > the only field that actually writes a correct value
    >
    > here's the code:
    >
    >
    thersitz, Jan 31, 2007
    #2
    1. Advertising

  3. Hi there,

    Use

    ControlParameter instead of FormParameter. The difference is that
    FormParameter takes its value directly from Request.Form collection using the
    name given by FormField. The problem with your vode is that, textbox does not
    post its value in Request.Form[textBox.ID] but in
    Request.Form[textBox.UniqueID] which reflects IDs of the parent controls.
    Change you insertparameters declaration to:

    <InsertParameters>
    <asp:ControlParameter Name="lastName" Type="String" ControlID="LastName"
    PropertyName="Text"/>
    <asp:ControlParameter Name="firstName" ControlID="FirstName" Type="String"
    PropertyName="Text"/>
    <asp:ControlParameter Name="address" Type="String" ControlID="Address"
    PropertyName="Text"/>
    <asp:ControlParameter Name="city" Type="String" ControlID="City"
    PropertyName="Text"/>
    </InsertParameters>

    --
    Milosz


    "thersitz" wrote:

    > using VStudio 2005/sql server 2005
    >
    > Have a simple web form that inserts the results in a table. It seems to
    > write to the table, but not the values from the asp forms fields. It adds a
    > new record and increments the id field by one -- but in all the other
    > fields, it merely writes Null to the fields. He are some other points:
    >
    > I am not inserting data into every field -- for test purposes I am only
    > using 4 fields.
    > The id field is NOT one of the fields on the asp form -- although it is the
    > only field that actually writes a correct value
    >
    > here's the code:
    >
    > <asp:TextBox ID="FirstName" runat="server"></asp:TextBox><br />
    > Last name:
    > <asp:TextBox ID="LastName" runat="server"></asp:TextBox><br />
    > Address:
    > <asp:TextBox ID="Address" runat="server"></asp:TextBox><br />
    > City:
    > <asp:TextBox ID="City" runat="server"></asp:TextBox><br />
    > Year created:
    > <asp:DropDownList ID="YearCreated" runat="server">
    > </asp:DropDownList><br />
    >
    > <asp:Button ID="Save" runat="server" Text="Save" />
    > </div>
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    > ConnectionString=
    > "<%$ ConnectionStrings:WHCConnectionString %>"
    > InsertCommand=
    > "INSERT INTO
    > [artsfestival] ([lastName], [firstName], [address], [city])
    > VALUES
    > (@lastName, @firstName, @address, @city)">
    > <InsertParameters>
    > <asp:FormParameter Name="lastName" Type="String"
    > FormField="LastName" />
    > <asp:FormParameter Name="firstName" Type="String"
    > FormField="FirstName" />
    > <asp:FormParameter Name="address" Type="String"
    > FormField="Address" />
    > <asp:FormParameter Name="city" Type="String"
    > FormField="City" />
    > </InsertParameters>
    > </asp:SqlDataSource>
    >
    >
    >
    >
    > "thersitz" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > using VStudio 2005/sql server 2005
    > >
    > > Have a simple web form that inserts the results in a table. It seems to
    > > write to the table, but not the values from the asp forms fields. It adds
    > > a new record and increments the id field by one -- but in all the other
    > > fields, it merely writes Null to the fields. He are some other points:
    > >
    > > I am not inserting data into every field -- for test purposes I am only
    > > using 4 fields.
    > > The id field is NOT one of the fields on the asp form -- although it is
    > > the only field that actually writes a correct value
    > >
    > > here's the code:
    > >
    > >

    >
    >
    >
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Jan 31, 2007
    #3
  4. thersitz

    thersitz Guest

    Thanks Milosz, it worked.

    I'm confused why the book had me use the FormParameter and FormFieldID --
    but thanks for getting me past this point.

    Take care.


    "Milosz Skalecki [MCAD]" <> wrote in message
    news:...
    > Hi there,
    >
    > Use
    >
    > ControlParameter instead of FormParameter. The difference is that
    > FormParameter takes its value directly from Request.Form collection using
    > the
    > name given by FormField. The problem with your vode is that, textbox does
    > not
    > post its value in Request.Form[textBox.ID] but in
    > Request.Form[textBox.UniqueID] which reflects IDs of the parent controls.
    > Change you insertparameters declaration to:
    >
    > <InsertParameters>
    > <asp:ControlParameter Name="lastName" Type="String" ControlID="LastName"
    > PropertyName="Text"/>
    > <asp:ControlParameter Name="firstName" ControlID="FirstName" Type="String"
    > PropertyName="Text"/>
    > <asp:ControlParameter Name="address" Type="String" ControlID="Address"
    > PropertyName="Text"/>
    > <asp:ControlParameter Name="city" Type="String" ControlID="City"
    > PropertyName="Text"/>
    > </InsertParameters>
    >
    > --
    > Milosz
    >
    >
    > "thersitz" wrote:
    >
    >> using VStudio 2005/sql server 2005
    >>
    >> Have a simple web form that inserts the results in a table. It seems to
    >> write to the table, but not the values from the asp forms fields. It adds
    >> a
    >> new record and increments the id field by one -- but in all the other
    >> fields, it merely writes Null to the fields. He are some other points:
    >>
    >> I am not inserting data into every field -- for test purposes I am only
    >> using 4 fields.
    >> The id field is NOT one of the fields on the asp form -- although it is
    >> the
    >> only field that actually writes a correct value
    >>
    >> here's the code:
    >>
    >> <asp:TextBox ID="FirstName" runat="server"></asp:TextBox><br />
    >> Last name:
    >> <asp:TextBox ID="LastName" runat="server"></asp:TextBox><br />
    >> Address:
    >> <asp:TextBox ID="Address" runat="server"></asp:TextBox><br />
    >> City:
    >> <asp:TextBox ID="City" runat="server"></asp:TextBox><br />
    >> Year created:
    >> <asp:DropDownList ID="YearCreated" runat="server">
    >> </asp:DropDownList><br />
    >>
    >> <asp:Button ID="Save" runat="server" Text="Save" />
    >> </div>
    >> <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    >> ConnectionString=
    >> "<%$ ConnectionStrings:WHCConnectionString %>"
    >> InsertCommand=
    >> "INSERT INTO
    >> [artsfestival] ([lastName], [firstName], [address], [city])
    >> VALUES
    >> (@lastName, @firstName, @address, @city)">
    >> <InsertParameters>
    >> <asp:FormParameter Name="lastName" Type="String"
    >> FormField="LastName" />
    >> <asp:FormParameter Name="firstName" Type="String"
    >> FormField="FirstName" />
    >> <asp:FormParameter Name="address" Type="String"
    >> FormField="Address" />
    >> <asp:FormParameter Name="city" Type="String"
    >> FormField="City" />
    >> </InsertParameters>
    >> </asp:SqlDataSource>
    >>
    >>
    >>
    >>
    >> "thersitz" <> wrote in message
    >> news:...
    >> > Hi,
    >> >
    >> > using VStudio 2005/sql server 2005
    >> >
    >> > Have a simple web form that inserts the results in a table. It seems to
    >> > write to the table, but not the values from the asp forms fields. It
    >> > adds
    >> > a new record and increments the id field by one -- but in all the other
    >> > fields, it merely writes Null to the fields. He are some other points:
    >> >
    >> > I am not inserting data into every field -- for test purposes I am only
    >> > using 4 fields.
    >> > The id field is NOT one of the fields on the asp form -- although it is
    >> > the only field that actually writes a correct value
    >> >
    >> > here's the code:
    >> >
    >> >

    >>
    >>
    >>
    thersitz, Jan 31, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page