Link to home
Start Free TrialLog in
Avatar of Si_Hibbard
Si_HibbardFlag for United States of America

asked on

problem with detailsview

I have details view in insertmode by default so the user can enter some data and for it to be inserted into the DB. But i get this error when i click the insert button:

Exception Details: System.Data.OleDb.OleDbException: Must declare the variable '@CompanyName'.

As you can see I have declared the vairiable as an insertitem parameter - whats wrong?
Code
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    <asp:DetailsView DefaultMode="Insert" ID="LoginDetailsView" runat="server"
    Height="50px" Width="125px" DataSourceID="SqlDataSource1" AutoGenerateRows="False">
        <Fields>
            <asp:TemplateField HeaderText="Name of Company">
                <EditItemTemplate>
                    <asp:TextBox ID="CompanyName" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="CompanyName" runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("CompanyName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Contact Name">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ContactName") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ContactName") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("ContactName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Telephone No.">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Telephone") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Telephone") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Telephone") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Email">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField CancelText="" DeleteText="" EditText="" InsertText="Submit Details"
                NewText="" SelectText="" ShowInsertButton="True" UpdateText="" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:VS_ConnectionString %>"
        ProviderName="<%$ ConnectionStrings:VS_ConnectionString.ProviderName %>"
        InsertCommand="INSERT INTO [LPlus2007UserTable] ([CompanyName], [ContactName], [Telephone], [Email]) VALUES (@CompanyName, @ContactName, @Telephone, @Email)">
        <InsertParameters>
            <asp:Parameter Name="CompanyName" Type="String" />
            <asp:Parameter Name="ContactName" Type="String" />
            <asp:Parameter Name="Telephone" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>
Avatar of stanscott2
stanscott2

I think the problem here is that you're using an ID of "CompanyName" in your TemplateField, but ALSO using that exact name in the <InsertParameters> section.  Change the IDs of your textboxes from CompanyName to companyName, and see whether that does the trick.
Avatar of Si_Hibbard

ASKER

I already tried that - named them something completely different but still get the error.
ASKER CERTIFIED SOLUTION
Avatar of stanscott2
stanscott2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Si Hibbard,
If you use parameter, you need to send them in code-behind.
Try:
<asp:ControlParameter ControlID="CompanyName" Name="CName" PropertyName="Text" Type="String" />
instead of:
<asp:Parameter Name="CompanyName" Type="String" />
Regards.
Thanks guys, it was the mixup with OLEDB causing the problem.