Solved

problem with detailsview

Posted on 2007-03-25
5
346 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

791 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