newbie: inserting data into sql form writes only null values

T

thersitz

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

thersitz

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

Guest

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

thersitz

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top