Solved

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

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
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
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
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.

690 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