Solved

SqlDataSource: SqlExcepion upon databinding: Must declare the scalar variable...

Posted on 2007-11-27
4
2,085 Views
Last Modified: 2011-10-03
I am programmatically creating a SqlDataSource as in the attached code.  However, when I run the code, where I call the DataBind I get a "SqlException: Must declare the scalar variable @prefix, Must declare the scalar variable @UserId"

However, in debug mode, I can see that the two parameters are actually populated in the SelectParameters collection.

What am I missing?
//The function that creates the datasource:

        public static SqlDataSource  CreateDataSource(string connectionName, MembershipUser user, string prefix)

        {

            string connectionString = WebConfigurationManager.ConnectionStrings[connectionName].ConnectionString;

            string query = "SELECT Column1 As AnchorText, @prefix + Column2 As FullUrl FROM table1 where RoleId IN (select RoleId from vw_aspnet_UsersInRoles WHERE UserId = @UserId)";

            SqlDataSource source = new SqlDataSource(connectionString, query);

            source.DataSourceMode = SqlDataSourceMode.DataReader;

            source.SelectParameters.Add("@prefix", TypeCode.String, prefix);

            source.SelectParameters.Add("@UserId", TypeCode.String, user.ProviderUserKey.ToString());

            return source;

        }
 

//The .aspx file

    <asp:Repeater ID="ApplicationMenu" runat="server">

        <ItemTemplate>

            <a href="<%# ((System.Data.DataRowView)Container.DataItem)[FullUrl] %>"><%# ((System.Data.DataRowView)Container.DataItem)["AnchorText"] %></a>

        </ItemTemplate>

    </asp:Repeater>
 

//The Codebehind file.

    public partial class Menu : System.Web.UI.Page

    {

        protected SqlDataSource source;

        protected void Page_Load(object sender, EventArgs e)

        {

            source = DataSourceFactory.CreateDataSource("Connection", Membership.GetUser(), "http://someserver.com/");

            ApplicationMenu.DataSource = source;

            ApplicationMenu.DataBind(); // ****** Error Happens Here ******

        }

    }

Open in new window

0
Comment
Question by:davisnw
4 Comments
 
LVL 16

Expert Comment

by:MikeMCSD
ID: 20362799
Not sure, but try changing this:
  "SELECT Column1 As AnchorText, @prefix + Column2 As FullUrl
to this:
  "SELECT Column1 As AnchorText, @prefix + Column2 As 'FullUrl'

I put single quotes around FullUrl
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20362816
try remove the @ symbols:
           source.SelectParameters.Add("prefix", TypeCode.String, prefix);
            source.SelectParameters.Add("UserId", TypeCode.String, user.ProviderUserKey.ToString());
 
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20363478
what about this:
//The function that creates the datasource:
        public static SqlDataSource  CreateDataSource(string connectionName, MembershipUser user, string prefix)
        {
            string connectionString = WebConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
            string query = "SELECT Column1 As AnchorText, ? + Column2 As FullUrl FROM table1 where RoleId IN (select RoleId from vw_aspnet_UsersInRoles WHERE UserId = ? )";
            SqlDataSource source = new SqlDataSource(connectionString, query);
            source.DataSourceMode = SqlDataSourceMode.DataReader;
            source.SelectParameters.Add("prefix", TypeCode.String, prefix);
            source.SelectParameters.Add("UserId", TypeCode.String, user.ProviderUserKey.ToString());
            return source;

Open in new window

0
 
LVL 3

Author Comment

by:davisnw
ID: 20363512
Removing the @ symbols fixed it!  Thanks!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

706 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

19 Experts available now in Live!

Get 1:1 Help Now