Solved

problem with detailsview

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now