Solved

problem with detailsview

Posted on 2007-03-25
5
348 Views
Last Modified: 2010-04-16
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>
0
Comment
Question by:Si_Hibbard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:stanscott2
ID: 18789518
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.
0
 
LVL 10

Author Comment

by:Si_Hibbard
ID: 18789612
I already tried that - named them something completely different but still get the error.
0
 
LVL 15

Accepted Solution

by:
stanscott2 earned 500 total points
ID: 18789787
Okay, here's the problem.  You're mixing up SQL Server syntax with OLE syntax.  With SQL server, you use the named parameters:  @CompanyName, etc.  WIth OLEDB, you can't.

With OLEDB, your string has to be:

 InsertCommand="INSERT INTO [LPlus2007UserTable] ([CompanyName], [ContactName], [Telephone], [Email]) VALUES (?,?,?,?)"

However, you're setting a SQLDataSource, and you're getting an OLEDB error.  Whichever one you're using, you have to be consistent.

0
 
LVL 9

Expert Comment

by:noulouk
ID: 18789799
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.
0
 
LVL 10

Author Comment

by:Si_Hibbard
ID: 18790025
Thanks guys, it was the mixup with OLEDB causing the problem.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question