Solved

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

Posted on 2007-11-27
4
2,101 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
[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
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 143

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
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…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

749 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