none
Only assignment, call, increment, decrement, and new object expressions can be used as a statement

    Question

  • Hello everyone,

     

    I'm new and learning C#. i'm having issue below. I was told to the value need to be assign to variable but i'm not sure how to do that.

     

    I’m getting error:

    Error      1              Only assignment, call, increment, decrement, and new object expressions can be used as a statement            

     

    q_reader.GetSchemaTable().Columns["Entity"].MaxLength;

     

    private void Fillcmb_Entity()

            {

                if (!Changing)

                {

                    q_Command.Parameters.Clear();

                    q_Command.CommandText = "select e.Entity, e.Name from Entity as e join UserToEntity as U\r";

                    q_Command.CommandText += "on e.Entity = U.Entity\r";

                    q_Command.CommandText += "where e.Active = @Active\r";

                    q_Command.CommandText += "and U.UserName = @UserName\r";

                    q_Command.CommandText += "order by e.Entity\r";

                    q_Command.Parameters.Add("@Entity", SqlDbType.VarChar).Value = cmb_Entity.SelectedItem != null && cmb_Entity.SelectedItem.ToString().IndexOf(' ') > 0 ? cmb_Entity.SelectedItem.ToString().Substring(0, cmb_Entity.SelectedItem.ToString().IndexOf(' ')) :string.Empty;

                    q_Command.Parameters.Add("@Active", SqlDbType.Bit).Value = true;

                    q_Command.Parameters.Add("@username", SqlDbType.VarChar).Value = SqlUserName;

     

                    SqlDataReader q_Reader;

                    q_Reader = q_Command.ExecuteReader();

                    Changing = true;

                    q_reader.GetSchemaTable().Columns["Entity"].MaxLength;                 

     

                    while (q_Reader.Read())

                    {

                        cmb_Entity.Items.Add(q_Reader["Entity"].ToString() + " " + q_Reader["Name"].ToString());

                                       

                    }

                    Changing = false;

                    q_Reader.Close();

                  

                }

            }

    Tuesday, November 08, 2011 3:38 PM

Answers

  • For example

    int max=q_reader.GetSchemaTable().Columns["Entity"].MaxLength;

    Regards

    Tuesday, November 08, 2011 4:17 PM
  • You are accessing MaxLength for no apparent reason and that's what the compiler is complaining about. You can make the error go away either changing the value of the property or storing its value into a variable as in, for instance:

    var maxLength = q_reader.GetSchemaTable().Columns["Entity"].MaxLength;

    but before you do that, you should ask yourself why you are accessing the property in the first place. If you don't need that value anywhere, you can just remove the line.

     

    HTH
    --mc

    Tuesday, November 08, 2011 4:22 PM
  • What is the purpose of that line of code?  You're finding out what the max length is, and then you're immediately discarding that information without using it.  Why do you need to know the max length?  

    If you just want to store it as a variable, the syntax is:

    TypeOfVariable VariableName = ExpressionForTheValueToSet;

    For example:

    int i = 0;

    If MaxLength is an integer, than you can just write:

    int length = q_reader.GetSchemaTable().Columns["Entity"].MaxLength;  
    


    Tuesday, November 08, 2011 4:24 PM

All replies

  • For example

    int max=q_reader.GetSchemaTable().Columns["Entity"].MaxLength;

    Regards

    Tuesday, November 08, 2011 4:17 PM
  • You are accessing MaxLength for no apparent reason and that's what the compiler is complaining about. You can make the error go away either changing the value of the property or storing its value into a variable as in, for instance:

    var maxLength = q_reader.GetSchemaTable().Columns["Entity"].MaxLength;

    but before you do that, you should ask yourself why you are accessing the property in the first place. If you don't need that value anywhere, you can just remove the line.

     

    HTH
    --mc

    Tuesday, November 08, 2011 4:22 PM
  • What is the purpose of that line of code?  You're finding out what the max length is, and then you're immediately discarding that information without using it.  Why do you need to know the max length?  

    If you just want to store it as a variable, the syntax is:

    TypeOfVariable VariableName = ExpressionForTheValueToSet;

    For example:

    int i = 0;

    If MaxLength is an integer, than you can just write:

    int length = q_reader.GetSchemaTable().Columns["Entity"].MaxLength;  
    


    Tuesday, November 08, 2011 4:24 PM