Solved

Must declare the scalar variable "@ScenarioID"

Posted on 2009-04-06
5
1,815 Views
Last Modified: 2012-05-06
[SqlException (0x80131904): Must declare the scalar variable "@ScenarioID".]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1950890
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4846875
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2392
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +192
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +317
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EditExample_formview.aspx.cs" Inherits="EditExample" %>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<script runat="server">
 

    protected void Page_Load(object sender, EventArgs e)

    {

        string connString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SADB;Data Source=KENLAPTOP\\sqlexpress";
 

            sqlDataSource1.ConnectionString = connString;

        // AccessDataSource1.DataFile = "~/App_Data/AccountInfo.mdb";

            sqlDataSource1.SelectCommand = "SELECT Scenario.* from Scenario";

           // sqlDataSource1.UpdateCommand = "UPDATE Scenario SET BusinessStartYear =?, ReportingStart = ?, ReportingEnd = ?, FilterString = ? WHERE ScenarioID = ?";

                sqlDataSource1.DeleteCommand="DELETE FROM Scenario WHERE ScenarioID = @ScenarioID";

          //  AccessDataSource1.InsertCommand="INSERT xxx  = ?";

       //  InsertCommand="INSERT INTO [DMM_ArcWeb] ([ID], [Status], [MALLNAME], [MALLCODE], [StateName], [DMANAME], [CBSANAME], [Latitude], [Longitude], [Link]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"    
 

            sqlDataSource1.UpdateParameters.Add("BusinessStartYear", "DateTime");

            sqlDataSource1.UpdateParameters.Add("ReportingStart", "DateTime");

            sqlDataSource1.UpdateParameters.Add("ReportingEnd", "DateTime");

            sqlDataSource1.UpdateParameters.Add("FilterString", "String");

            sqlDataSource1.DeleteParameters.Add("ScenarioID", "Int32");

    }

</script>

1

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataKeyNames="ScenarioID" 

        DataSourceID="sqlDataSource1" PageSize="16" AllowSorting="True" AutoGenerateColumns="true" >

            <Columns>

                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

            </Columns>

        </asp:GridView>

        <asp:SqlDataSource ID="sqlDataSource1" runat="server">

        </asp:SqlDataSource>

 
 
 
 

        <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" 

            DataSourceID="sqlDataSource1" Height="50px" Width="125px" 

            AutoGenerateDeleteButton="True" 

            AutoGenerateInsertButton="True" 

            AutoGenerateEditButton="True">

            

        </asp:DetailsView>

 
 
 
 

    </form>

</body>

</html>

Open in new window

0
Comment
Question by:goodk
  • 3
  • 2
5 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24083597
When does this occur?  Which part of the SqlDataSource comes into play when you get that error?
0
 

Author Comment

by:goodk
ID: 24083754
sqlDataSource1.DeleteCommand="DELETE FROM Scenario WHERE ScenarioID = @ScenarioID";

Kindly, guide me to improve the code, this code worked for Access but I am having hard time with sql.  Please also see Update and Insert
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24086389
Here is a possibility:

Must declare the scalar variable @ID
http://fabdata.wordpress.com/2007/03/23/must-declare-the-scalar-variable-id/
0
 

Author Comment

by:goodk
ID: 24093447
           GridView1.DataKeyNames="ScenarioID";
does not work!
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 24098991
You are adding DeleteCommand parameters in code, and not in HTML, so I believe that you need to add the '@', which gets handled by the SqlDataSource automagically.

            sqlDataSource1.UpdateParameters.Add("@BusinessStartYear", "DateTime");
            sqlDataSource1.UpdateParameters.Add("@ReportingStart", "DateTime");
            sqlDataSource1.UpdateParameters.Add("@ReportingEnd", "DateTime");
            sqlDataSource1.UpdateParameters.Add("@FilterString", "String");
            sqlDataSource1.DeleteParameters.Add("@ScenarioID", "Int32");
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert to Hour/minute textbox and compare 3 52
asp.net mvc return json 2 53
Name space syntax error 12 40
Show/Hide ASP.NET controls with JQuery best practices 3 14
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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…
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 …
Send secure, cloud-based, encrypted alerts and maintain HIPAA compliant messaging. Integrates priority & secure messaging into one application. Ensures IT, emergency respondents and healthcare professionals that their critical messages are never mis…

911 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

18 Experts available now in Live!

Get 1:1 Help Now