Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7193915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:15:57+00:00 2026-05-28T20:15:57+00:00

I am trying to bind data to a label when it is the same

  • 0

I am trying to bind data to a label when it is the same as the query string that I have.
I keep getting a error saying.

Must declare the scalar variable “@YouthClubID”.

Here is my code.

      using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;


namespace WebApplication1
{
    public partial class youthclubpage : System.Web.UI.Page
    {
        int YouthClubID;

       protected void Page_Load(object sender, EventArgs e)
        {
            using (SqlConnection sqlConnection = new SqlConnection("server = sql7.hostinguk.net; uid = joanton7865org7272_phelim; pwd = Armo32nk; database = joanton7865org7272_youthpodcast;"))
            {
                sqlConnection.Open();
                string strSQL = "Select youthclubname from youthclublist WHERE ([YouthClubID] = @YouthClubID)";
                using (SqlCommand sqlCommand = new SqlCommand(strSQL, sqlConnection))
                {
                    sqlCommand.Parameters.AddWithValue("@YouthClubID", Request.QueryString["club"]);
                    txtClubName.Text = sqlCommand.ExecuteScalar() + "";
                }
            }
        }
        }


    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:15:59+00:00Added an answer on May 28, 2026 at 8:15 pm

    The error happens, as others said already, because you’re not populating the Parameter defined in the SQL statement.

    Anyway, you don’t need Adapter at all, in your case ExecuteScalar method is enough:

    using (SqlConnection sqlConnection = new SqlConnection("..."))
    {
        sqlConnection.Open();
        string strSQL = "Select youthclubname from youthclublist WHERE ([YouthClubID] = @YouthClubID)";
        using (SqlCommand sqlCommand = new SqlCommand(strSQL, sqlConnection))
        {
            sqlCommand.Parameters.AddWithValue("@YouthClubID", Request.QueryString["club"]);
            txtClubName.Text = sqlCommand.ExecuteScalar() + "";
        }
    }
    

    In the above I assumed the club ID is passed as page.aspx?club=[id here] meaning the query string variable name is “club”, change to the real name of course.

    The + "" part is meant to put empty text in case there is no matching value in database.
    When there is no such value, sqlCommand.ExecuteScalar() will return null – having the plain way of:

    txtClubName.Text = sqlCommand.ExecuteScalar().ToString();
    

    Will generate error in such case – by adding empty string the conversion to string occurs automatically and it looks more pretty. See this question as well. Needless to say, if the value is not null, it won’t be changed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to find out how to bind data to a System.Windows.Forms.DomainUpDown()
I'm trying to bind a XAML ComboBox so that its list items are the
I am trying to bind a label 2 (or more!) fields in a dataset
I've been kinda stuck in trying to organize data and bind them to a
When trying to use SPWeb.GetSiteData(SPSiteDataQuery) (Trying to bind some data to SPGridView), it throws
Ok guys, so I am trying to bind data into a dropdown list from
I have two Live Wallpapers that belong to the same Application and I am
I am trying to bind the data from linq xml in isolated storage. The
I'm trying to use LINQ to bind the data in my GridView. In my
I'm trying to extend ButtonBarButton to include an additional label that shows the number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.