Thursday, March 29, 2012

newbie: inserting data into sql form writes only null values

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: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" <thersitz@.gmail.comwrote in message
news:OhRMtSXRHHA.2256@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

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:
>
>


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:

Quote:

Originally Posted by

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" <thersitz@.gmail.comwrote in message
news:OhRMtSXRHHA.2256@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

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:


>
>
>


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]" <mily242@.REMOVEITwp.plwrote in message
news:1206D448-EDF2-419B-9A48-D05F7B51FAAF@.microsoft.com...

Quote:

Originally Posted by

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:
>

Quote:

Originally Posted by

> 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" <thersitz@.gmail.comwrote in message
>news:OhRMtSXRHHA.2256@.TK2MSFTNGP02.phx.gbl...

Quote:

Originally Posted by

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:
>
>


>>
>>
>>

0 comments:

Post a Comment