locked
Must declare the variable '@Name' problem RRS feed

  • Question

  • User-1619164162 posted

    I am getting an error when I click insert on the web page

    Must declare the variable '@Name'


    Can someone help me with this error


    This is my aspx file

     

    <%@ Page Language="C#"  AutoEventWireup="true" CodeBehind="CreateWorkOrder.aspx.cs" Inherits="Capstone.CreateWorkOrder" %>


    <!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 id="Head1" runat="server">

        <title>Work Order</title>
        <style type="text/css">
            .style1
            {
                width: 756px;
                height: 78px;
            }
        </style>
        </head>
    <body>
      <form id="form1" runat="server">
        <img alt="" class="style1" src="Styles/skuselector2[1].gif" /><br />
        <br />

        <br />
        Customer Information<br />
        <br />

     
        <asp:FormView ID="FormViewCreate" runat="server" CellPadding="4"
            ForeColor="#333333" DataSourceID="SqlDataSource1">
            <EditItemTemplate>
                WorkOrderID:
                <asp:TextBox ID="txtWorkOrderID" runat="server"
                    Text='<%# Bind("WorkOrderID") %>' />
                <br />
                Name:
                <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' />
                <br />
                Address:
                <asp:TextBox ID="txtAddress" runat="server" Text='<%# Bind("Address") %>' />
                <br />
                City:
                <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("City") %>' />
                <br />
                State:
                <asp:TextBox ID="txtState" runat="server" Text='<%# Bind("State") %>' />
                <br />
                Zip Code:
                <asp:TextBox ID="txtZipCode" runat="server" Text='<%# Bind("ZipCode") %>' />
                <br />
                Phone:
                <asp:TextBox ID="txtPhone" runat="server" Text='<%# Bind("Phone") %>' />
                <br />
                Due Date:
                <asp:TextBox ID="txtDueDate" runat="server" Text='<%# Bind("DueDate") %>' />
                <br />
                Employee Name:
                <asp:TextBox ID="txtEmployeeName" runat="server"
                    Text='<%# Bind("EmployeeName") %>' />
                <br />
                <br />
                <br />
                Copies:
                <asp:ListBox ID="lstCopies" runat="server" Text='<%# Bind("Copies") %>' >
                    <asp:ListItem>Black & White</asp:ListItem>
                    <asp:ListItem>Color</asp:ListItem>
                </asp:ListBox>
                <br />
                Paper Size:
                <asp:ListBox ID="lstPaperSize" runat="server"
                    Text='<%# Bind("PaperSize") %>' >
                    <asp:ListItem>8 1/2 x 11</asp:ListItem>
                    <asp:ListItem>8 1/2 x 14</asp:ListItem>
                    <asp:ListItem>11 x 17</asp:ListItem>
                    <asp:ListItem>18 x 24</asp:ListItem>
                    <asp:ListItem>36 x 48</asp:ListItem>
                    <asp:ListItem>40 x 60</asp:ListItem>
                </asp:ListBox>
                <br />
                Paper Code:
                <asp:ListBox ID="lstPaperCode" runat="server"
                    Text='<%# Bind("PaperCode") %>' >
                    <asp:ListItem>20#</asp:ListItem>
                    <asp:ListItem>24#</asp:ListItem>
                    <asp:ListItem>65#</asp:ListItem>
                    <asp:ListItem>80#</asp:ListItem>
                    <asp:ListItem>80# Gloss Text</asp:ListItem>
                    <asp:ListItem>80# Gloss Cover</asp:ListItem>
                </asp:ListBox>
                <br />
                Sides:
                <asp:ListBox ID="lstSides" runat="server" Text='<%# Bind("Sides") %>' >
                    <asp:ListItem>1-1</asp:ListItem>
                    <asp:ListItem>1-2</asp:ListItem>
                    <asp:ListItem>2-1</asp:ListItem>
                    <asp:ListItem>2-2</asp:ListItem>
                </asp:ListBox>
                <br />
                Binding:
                <asp:ListBox ID="lstBinding" runat="server" Text='<%# Bind("Binding") %>' >
                    <asp:ListItem>4mm Coil</asp:ListItem>
                    <asp:ListItem>4mm Comb</asp:ListItem>
                    <asp:ListItem>6mm Coil</asp:ListItem>
                    <asp:ListItem>6mm Comb</asp:ListItem>
                    <asp:ListItem>8mm Coil</asp:ListItem>
                    <asp:ListItem>8mm Comb</asp:ListItem>
                    <asp:ListItem>10mm Coil</asp:ListItem>
                    <asp:ListItem>10mm Comb</asp:ListItem>
                </asp:ListBox>
                <br />
                Binding Cover:
                <asp:ListBox ID="lstBindingCover" runat="server"
                    Text='<%# Bind("BindingCover") %>' >
                    <asp:ListItem>Black Grain</asp:ListItem>
                    <asp:ListItem>Black Vinyl</asp:ListItem>
                    <asp:ListItem>Blue Grain</asp:ListItem>
                    <asp:ListItem>Blue Vinyl</asp:ListItem>
                    <asp:ListItem>Red Grain</asp:ListItem>
                    <asp:ListItem>Red Vinyl</asp:ListItem>
                    <asp:ListItem>White Grain</asp:ListItem>
                    <asp:ListItem>White Vinyl</asp:ListItem>
                </asp:ListBox>
                <br />
                Stapling:
                <asp:ListBox ID="lstStapling" runat="server"
                    Text='<%# Bind("Stapling") %>' >
                    <asp:ListItem>1 Left</asp:ListItem>
                    <asp:ListItem>1 Right</asp:ListItem>
                    <asp:ListItem>2 Left</asp:ListItem>
                    <asp:ListItem>2 Right</asp:ListItem>
                </asp:ListBox>
                <br />
                Folding:
                <asp:ListBox ID="lstFolding" runat="server" Text='<%# Bind("Folding") %>' >
                    <asp:ListItem>Half-fold</asp:ListItem>
                    <asp:ListItem>Tri-fold</asp:ListItem>
                    <asp:ListItem>Z-fold</asp:ListItem>
                </asp:ListBox>
                <br />
                Desktop Publishing:
                <asp:ListBox ID="lstDP" runat="server"
                    Text='<%# Bind("DesktopPublishing") %>' >
                    <asp:ListItem>1 minute</asp:ListItem>
                    <asp:ListItem>1 hour</asp:ListItem>
                    <asp:ListItem>Scanning</asp:ListItem>
                </asp:ListBox>
                <br />
                File Upload:&nbsp;
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <br />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
                    CommandName="Update" Text="Update" />
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <InsertItemTemplate>
                WorkOrderID:
                <asp:TextBox ID="txtWorkOrderID" runat="server"
                    Text='<%# Bind("WorkOrderID") %>' />
                <br />
                Name:
                <asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' />
                <br />
                Address:
                <asp:TextBox ID="txtAddress" runat="server" Text='<%# Bind("Address") %>' />
                <br />
                City:
                <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("City") %>' />
                <br />
                State:
                <asp:TextBox ID="txtState" runat="server" Text='<%# Bind("State") %>' />
                <br />
                ZipCode:
                <asp:TextBox ID="txtZipCode" runat="server" Text='<%# Bind("ZipCode") %>' />
                <br />
                Phone:
                <asp:TextBox ID="txtPhone" runat="server" Text='<%# Bind("Phone") %>' />
                <br />
                DueDate:
                <asp:TextBox ID="txtDueDate" runat="server" Text='<%# Bind("DueDate") %>' />
                <br />
                EmployeeName:
                <asp:TextBox ID="txtEmployeeName" runat="server"
                    Text='<%# Bind("EmployeeName") %>' />
                <br />
                Copies:
                <asp:ListBox ID="lstCopies" runat="server" Text='<%# Bind("Copies") %>' >
                    <asp:ListItem>Black & White</asp:ListItem>
                    <asp:ListItem>Color</asp:ListItem>
                </asp:ListBox>
                <br />
                PaperSize:
                <asp:ListBox ID="lstPaperSize" runat="server"
                    Text='<%# Bind("PaperSize") %>'>
                    <asp:ListItem>8 1/2 x 11</asp:ListItem>
                    <asp:ListItem>8 1/2 x 14</asp:ListItem>
                    <asp:ListItem>11 x 17</asp:ListItem>
                    <asp:ListItem>18 x 24</asp:ListItem>
                    <asp:ListItem>36 x 48</asp:ListItem>
                    <asp:ListItem>40 x 60</asp:ListItem>
                </asp:ListBox>
                   
                <br />
                PaperCode:
                <asp:ListBox ID="lstPaperCode" runat="server"
                    Text='<%# Bind("PaperCode") %>' >
                    <asp:ListItem>20#</asp:ListItem>
                    <asp:ListItem>24#</asp:ListItem>
                    <asp:ListItem>65#</asp:ListItem>
                    <asp:ListItem>80#</asp:ListItem>
                    <asp:ListItem>80# Gloss Text</asp:ListItem>
                    <asp:ListItem>80# Gloss Cover</asp:ListItem>
                </asp:ListBox>
                <br />
                Sides:
                <asp:ListBox ID="lstSides" runat="server" Text='<%# Bind("Sides") %>' >
                    <asp:ListItem>1-1</asp:ListItem>
                    <asp:ListItem>1-2</asp:ListItem>
                    <asp:ListItem>2-1</asp:ListItem>
                    <asp:ListItem>2-2</asp:ListItem>
                </asp:ListBox>
                <br />
                Binding:
                <asp:ListBox ID="lstBinding" runat="server" Text='<%# Bind("Binding") %>' >
                    <asp:ListItem>4mm Coil</asp:ListItem>
                    <asp:ListItem>4mm Comb</asp:ListItem>
                    <asp:ListItem>6mm Coil</asp:ListItem>
                    <asp:ListItem>6mm Comb</asp:ListItem>
                    <asp:ListItem>8mm Coil</asp:ListItem>
                    <asp:ListItem>8mm Comb</asp:ListItem>
                    <asp:ListItem>10mm Coil</asp:ListItem>
                    <asp:ListItem>10mm Comb</asp:ListItem>
                </asp:ListBox>
                <br />
                BindingCover:
                <asp:ListBox ID="lstBindingCover" runat="server"
                    Text='<%# Bind("BindingCover") %>' >
                    <asp:ListItem>Black Grain</asp:ListItem>
                    <asp:ListItem>Black Vinyl</asp:ListItem>
                    <asp:ListItem>Blue Grain</asp:ListItem>
                    <asp:ListItem>Blue Vinyl</asp:ListItem>
                    <asp:ListItem>Red Grain</asp:ListItem>
                    <asp:ListItem>Red Vinyl</asp:ListItem>
                    <asp:ListItem>White Grain</asp:ListItem>
                    <asp:ListItem>White Vinyl</asp:ListItem>
                </asp:ListBox>
                <br />
                Stapling:
                <asp:ListBox ID="lstStapling" runat="server"
                    Text='<%# Bind("Stapling") %>' >
                    <asp:ListItem>1 Left</asp:ListItem>
                    <asp:ListItem>1 Right</asp:ListItem>
                    <asp:ListItem>2 Left</asp:ListItem>
                    <asp:ListItem>2 Right</asp:ListItem>
                </asp:ListBox>
                <br />
                Folding:
                <asp:ListBox ID="lstFolding" runat="server" Text='<%# Bind("Folding") %>' >
                    <asp:ListItem>Half-fold</asp:ListItem>
                    <asp:ListItem>Tri-fold</asp:ListItem>
                    <asp:ListItem>Z-fold</asp:ListItem>
                </asp:ListBox>
                <br />
                DesktopPublishing:
                <asp:ListBox ID="lstDP" runat="server"
                    Text='<%# Bind("DesktopPublishing") %>' >
                    <asp:ListItem>1 minute</asp:ListItem>
                    <asp:ListItem>1 hour</asp:ListItem>
                    <asp:ListItem>Scanning</asp:ListItem>
                </asp:ListBox>
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
                    CommandName="Insert" Text="Insert" />
                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </InsertItemTemplate>
            <ItemTemplate>
                WorkOrderID:&nbsp;
                <asp:Label ID="WorkOrderIDLabel" runat="server"
                    Text='<%# Bind("WorkOrderID") %>' />
                <br />
                Name:
                <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>' />
                <br />
                Address:
                <asp:Label ID="AddressLabel" runat="server" Text='<%# Bind("Address") %>' />
                <br />
                City:
                <asp:Label ID="CityLabel" runat="server" Text='<%# Bind("City") %>' />
                <br />
                State:
                <asp:Label ID="StateLabel" runat="server" Text='<%# Bind("State") %>' />
                <br />
                ZipCode:
                <asp:Label ID="ZipCodeLabel" runat="server" Text='<%# Bind("ZipCode") %>' />
                <br />
                Phone:
                <asp:Label ID="PhoneLabel" runat="server" Text='<%# Bind("Phone") %>' />
                <br />
                DueDate:
                <asp:Label ID="DueDateLabel" runat="server" Text='<%# Bind("DueDate") %>' />
                <br />
                EmployeeName:
                <asp:Label ID="EmployeeNameLabel" runat="server"
                    Text='<%# Bind("EmployeeName") %>' />
                <br />
                Copies:
                <asp:Label ID="CopiesLabel" runat="server" Text='<%# Bind("Copies") %>' />
                <br />
                PaperSize:
                <asp:Label ID="PaperSizeLabel" runat="server" Text='<%# Bind("PaperSize") %>' />
                <br />
                PaperCode:
                <asp:Label ID="PaperCodeLabel" runat="server" Text='<%# Bind("PaperCode") %>' />
                <br />
                Sides:
                <asp:Label ID="SidesLabel" runat="server" Text='<%# Bind("Sides") %>' />
                <br />
                Binding:
                <asp:Label ID="BindingLabel" runat="server" Text='<%# Bind("Binding") %>' />
                <br />
                BindingCover:
                <asp:Label ID="BindingCoverLabel" runat="server"
                    Text='<%# Bind("BindingCover") %>' />
                <br />
                Stapling:
                <asp:Label ID="StaplingLabel" runat="server" Text='<%# Bind("Stapling") %>' />
                <br />
                Folding:
                <asp:Label ID="FoldingLabel" runat="server" Text='<%# Bind("Folding") %>' />
                <br />
                DesktopPublishing:
                <asp:Label ID="DesktopPublishingLabel" runat="server"
                    Text='<%# Bind("DesktopPublishing") %>' />
                <br />
                <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
                    CommandName="New" Text="New" />
            </ItemTemplate>
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        </asp:FormView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>"
            InsertCommand="INSERT INTO [Customer]  ([Name], [Address], [City], [State], [ZipCode], [Phone], [DueDate], [EmployeeName],  [Copies], [PaperSize], [PaperCode], [Sides], [Binding], [BindingCover], [Stapling], [Folding], [DesktopPublishing]) VALUES (@Name, @Address, @City, @State, @ZipCode, @DueDate, @EmployeeName, @Copies, @PaperSize, @PaperCode, @Sides, @Binding, @BindingCover, @Stapling, @Folding, @DesktopPublishing)"
            ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>"
            SelectCommand="SELECT WorkOrderID, Name, Address, City, State, ZipCode, Phone, DueDate, EmployeeName, Copies, PaperSize, PaperCode, Sides, Binding, BindingCover, Stapling, Folding, DesktopPublishing FROM dansari.dbo.Customer INNER JOIN dansari.dbo.WorkOrder ON Customer.CustomerID = WorkOrder.CustomerID">
        </asp:SqlDataSource>
        </form>
        </body>
    </html>

    Wednesday, April 20, 2011 7:38 PM

Answers

  • User1957418580 posted

    I'm sorry, we may have been leading you down the wrong path.  Since you have InsertItemTemplate, you should only need insertparameters for whatever items you don't have bound already in your itemtemplates.  Going back to your first post, I think the issue might be that your InsertItemTemplate has a "WorkOrderId" bound, but that parameter isn't listed in your insertCommand.  Can you start over with just one or two parameters and get that to work, and then add the others?  If it still fails with just one or two parameters, can you send the code that you are doing this small test with and the full error that you get?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 27, 2011 7:00 AM

All replies

  • User-1360095595 posted
    I don't see where you've declared the update parameters. You need to add an <updateparameters> section to your sqldatasource and specify all the parameters you're using as part of your update command. I'm sure you can Bingle the details.
    Wednesday, April 20, 2011 10:08 PM
  • User-1619164162 posted

    I put the <update parameters>, but it is still giving me that error.

    Wednesday, April 20, 2011 11:48 PM
  • User3866881 posted

    Well, I think since you are using "InsertCommand", So you must define the parameters in the <InsertParameters> tags. Something looks like this:

     <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>"
    InsertCommand="INSERT INTO [Customer] ([Name], [Address], [City], [State], [ZipCode], [Phone], [DueDate], [EmployeeName], [Copies], [PaperSize], [PaperCode], [Sides], [Binding], [BindingCover], [Stapling], [Folding], [DesktopPublishing]) VALUES (@Name, @Address, @City, @State, @ZipCode, @DueDate, @EmployeeName, @Copies, @PaperSize, @PaperCode, @Sides, @Binding, @BindingCover, @Stapling, @Folding, @DesktopPublishing)" …………>

    <InsertParameters>

          <asp:Parameter Name="Name" Type="String" DefaultValue="Your Value Here"/>

    </InsertParameters>

    </asp:SqlDataSource>

    Saturday, April 23, 2011 8:35 PM
  • User-1619164162 posted

    I did what you suggested, but it is still saying the same thing.

    Saturday, April 23, 2011 9:04 PM
  • User3866881 posted

    Hi again:)

    I notice that you haven't defined the primary key to the public property of the FormView called "DataKeys". You must specify it

     <asp:FormView ID="FormViewCreate" runat="server" CellPadding="4"
    ForeColor="#333333" DataSourceID="SqlDataSource1" DataKeys="????">

    If still fails, please rename the "Name" to something like "UserName" instead, for Name is a key word in SQL.

    Anyway, have you written anything in the code-behind?

    Thx

    Saturday, April 23, 2011 11:24 PM
  • User-1619164162 posted

    It is still giving me the same error

    Sunday, April 24, 2011 12:22 PM
  • User1957418580 posted

    Can you show us what you have for the insertParameter.  Did you specify the proper controlParameter like (in your case):

     <InsertParameters>

     <asp:ControlParameter ControlID="txtName" Name="Name" PropertyName="Text" />

    ...

    </InsertParameters>

    Monday, April 25, 2011 7:18 AM
  • User198012145 posted

    you need to pass paramter to your insert query since I am not getting any parameter that you have passed to pass parameter in your sqlDatasource

     

    you need to use it like

     

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString3 %>"
            InsertCommand="INSERT INTO [Customer]  ([Name], [Address], [City], [State], [ZipCode], [Phone], [DueDate], [EmployeeName],  [Copies], [PaperSize], [PaperCode], [Sides], [Binding], [BindingCover], [Stapling], [Folding], [DesktopPublishing]) VALUES (@Name, @Address, @City, @State, @ZipCode, @DueDate, @EmployeeName, @Copies, @PaperSize, @PaperCode, @Sides, @Binding, @BindingCover, @Stapling, @Folding, @DesktopPublishing)"
            ProviderName="<%$ ConnectionStrings:ConnectionString3.ProviderName %>"
            SelectCommand="SELECT WorkOrderID, Name, Address, City, State, ZipCode, Phone, DueDate, EmployeeName, Copies, PaperSize, PaperCode, Sides, Binding, BindingCover, Stapling, Folding, DesktopPublishing FROM dansari.dbo.Customer INNER JOIN dansari.dbo.WorkOrder ON Customer.CustomerID = WorkOrder.CustomerID">

    <InsertParameters>
                            <asp:Parameter Name="Name" DbType="Int32"  />
     </InsertParameters>
        </asp:SqlDataSource>

     

    you can use it like this way or you can simply use event onInserting event of sqlDatasource where you can assign values to your parameter

     

    here is an example of code behind

     

    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceCommandEventArgs e)
        {
                e.Command.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Name", 'Megh'));
        }

    Monday, April 25, 2011 7:25 AM
  • User-1619164162 posted

    When I do Control Parameter, this is what I get

     

    Could not find control 'txtName' in ControlParameter 'Name'.

    Tuesday, April 26, 2011 10:15 AM
  • User-1619164162 posted

    When I do what you suggested, I get this error.

     

    Error    1    Too many characters in character literal   

     

    Tuesday, April 26, 2011 10:21 AM
  • User3866881 posted

    Hi again:)

    <InsertParameters>

    <asp:ControlParameter ControlID="txtName" Name="Name" PropertyName="Text" />  //This is only a sample. So please confirm that you have a TextBox outside on the page, and the textbox's Id should be "txtName":)

    ...

    </InsertParameters>

    Tuesday, April 26, 2011 9:16 PM
  • User1957418580 posted

    I'm sorry, we may have been leading you down the wrong path.  Since you have InsertItemTemplate, you should only need insertparameters for whatever items you don't have bound already in your itemtemplates.  Going back to your first post, I think the issue might be that your InsertItemTemplate has a "WorkOrderId" bound, but that parameter isn't listed in your insertCommand.  Can you start over with just one or two parameters and get that to work, and then add the others?  If it still fails with just one or two parameters, can you send the code that you are doing this small test with and the full error that you get?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 27, 2011 7:00 AM