Solved

problem with detailsview

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

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

630 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