Solved

'Must declare the scalar variable " Error with asp.net gridview

Posted on 2008-06-20
6
2,491 Views
Last Modified: 2010-04-21
I am trying to add the delete function to a grid view and am getting the 'Must declare the scalar variable "@SaleRef" error. I have looked at other forums and the common problem seems to be not delclaring the parameter and not having DataKeyNames set. I think I have done this (see code).

I've looked at this for so long I can't see where I've gone wrong...

Thanks for your help
<form id="form1" runat="server">
    <div>
<asp:SqlDataSource ID="SqlDataSource1" 
     Runat="server"
     ConnectionString="Server=mysqlserver; Database=mydb; User ID=myuserid; Password=mypass;"
					DeleteCommand="DELETE FROM [SaleRef] WHERE [SaleRef] = @SaleRef"
					SelectCommand="SELECT [SaleRef], [ClosingDate], [ClosingTime] FROM [SaleDetails]"
            providerName="System.Data.SqlClient">
			<DeleteParameters>
                <asp:Parameter Name="SaleRef" Type="Int32" />
            </DeleteParameters>
</asp:SqlDataSource>
 
    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="#FFE0C0"
            BorderColor="black" BorderStyle="Solid" BorderWidth="5px" DataKeyNames="SaleRef"
            DataSourceID="SqlDataSource1" EmptyDataText="There are no sales" Font-Names="Verdana"
            Font-Size="Small" GridLines="None">
			<Columns>
                <asp:CommandField ShowDeleteButton="True" ButtonType="Button" DeleteText="Delete Sale" />
				<asp:BoundField DataField="SaleRef" HeaderText="Sale #" />
                <asp:BoundField DataField="closingdate" HeaderText="Closing Date" HtmlEncode="False" />
                <asp:BoundField DataField="closingtime" HeaderText="Closing Time" HtmlEncode="False" />
            </Columns>
            <HeaderStyle BackColor="Green" Font-Size="small" Font-Underline="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="#E0E0E0" />
        </asp:GridView>
    </form>

Open in new window

0
Comment
Question by:kirkheaton25
  • 3
  • 2
6 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 21829956
Hello kirkheaton25,

                  <asp:Parameter Name="SaleRef" Type="Int32" />
Should be
                  <asp:Parameter Name="@SaleRef" Type="Int32" />

Regards,

TimCottee
0
 
LVL 1

Author Comment

by:kirkheaton25
ID: 21829999
Thanks, however after making that change I now get the message:
Invalid object name 'SaleRef'. Any ideas?
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21830010
Tim, I think he is ok with his declaration.

I think what you are missing is a way to declare your SaleRef, you never state which one to delete.

Usually this is in a SessionParameter, ControlParameter or QueryStringParameter.  Yours is simply a Parameter so the program does not know which one to delete.  You have to make sure you specify that.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:kirkheaton25
ID: 21830459
Hi Naspinski,
Isn't the SaleRef value being sent through the postback when I click the delete button?
(From the grid source code: __doPostBack('GridView1','Delete$2')".)
Is this a ControlParameter, it shouldn't be SessionParameter or QueryStringParameter should it?
0
 
LVL 21

Accepted Solution

by:
naspinski earned 500 total points
ID: 21830508
You are corerct, I didn't realize you were populating them automatically.  I think I see what the problem is.  Your DeleteCOmmand is: "DELETE FROM [SaleRef] WHERE [SaleRef] = @SaleRef"
But it looks like your DB is SaleDetails, so I think the SaleRef we are looking at is the first one, which is not correct at all.  It should be
DELETE FROM [SaleDetails] WHERE [SaleRef] = @SaleRef

Open in new window

0
 
LVL 1

Author Closing Comment

by:kirkheaton25
ID: 31469108
Many Thanks for your help. I was too busy looking at the paramters and got caught out with a schoolboy sql mistake!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on Add method 1 51
How to close an asp.net web form using C# ? 4 30
mail.To.Add 5 26
Need to start a web service from Visual Studio 2015 Pro 2 24
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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