Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…

732 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