none
C# Expression must be constant RRS feed

  • Question

  • Hi - I am learning C# for ASP.net webforms and have an problem with trying to read from an SQL DB. The code from my aspx.cs file is as follows - 

     protected void CheckUser_Click(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                int rows_returned;

                string credentials = ConfigurationManager.ConnectionStrings["uehcmdbConnection"].ConnectionString;

                const string sqlQuery = @"
                      select ShareholderCertificate,
                             ShareholderCertificateDate,
                             ShareholderShares,
                             ShareholderStatus
                      from uehcmdb.tblShareholder
                      where ShareholderCertificate=" + txtCertNum.Text  ;
                      
                using (SqlConnection connection = new SqlConnection(credentials))
                using (SqlCommand cmd = connection.CreateCommand())
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))

                {

    My issue is with the "where ShareholderCertificate=" clause.  The "ShareholderCertificate" column is an INT.  The variable "txtCertNum.Text" is from an <asp:TextBox... on the webform. I get the error "The expression being assigned to 'sqlQuery' must be a constant".   I tried a few things but can't seem to get it. 

    Any help would be appreciated. Thanks 

    Monday, December 23, 2019 1:28 PM

Answers

  • Use

    const string sqlQuery = @"
                      select ShareholderCertificate,
                             ShareholderCertificateDate,
                             ShareholderShares,
                             ShareholderStatus
                      from uehcmdb.tblShareholder
                      where ShareholderCertificate = @ShareholderCertificate" ;
    And use a parameter for @ShareholderCertificate which I placed into your code. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by CoolDadTxModerator Monday, December 23, 2019 3:07 PM
    • Marked as answer by ikeni Tuesday, December 24, 2019 12:00 AM
    Monday, December 23, 2019 1:38 PM
    Moderator

All replies

  • Use

    const string sqlQuery = @"
                      select ShareholderCertificate,
                             ShareholderCertificateDate,
                             ShareholderShares,
                             ShareholderStatus
                      from uehcmdb.tblShareholder
                      where ShareholderCertificate = @ShareholderCertificate" ;
    And use a parameter for @ShareholderCertificate which I placed into your code. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by CoolDadTxModerator Monday, December 23, 2019 3:07 PM
    • Marked as answer by ikeni Tuesday, December 24, 2019 12:00 AM
    Monday, December 23, 2019 1:38 PM
    Moderator
  • Thank You! Worked out just fine.

    Tuesday, December 24, 2019 12:00 AM