0

So here is my code. What I'm trying to do store double rate_num value to number variable. But it is still getting an error.

String conn = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
    string query = "select " + rate_from + " from Exchange_rate where ERates_Status = 'Active'";
    SqlConnection sqlcon = new SqlConnection(conn);
    sqlcon.Open();
    SqlCommand cmd = new SqlCommand(query, sqlcon);
    SqlDataReader reader = cmd.ExecuteReader();

    double number = "";

    while (reader.Read())
    {
        double rate_num = reader.GetDouble(0);

        number = rate_num
    }

    number;

    sqlcon.Close();
5
  • What is the error? Commented Jul 10, 2019 at 7:34
  • there is red underline in the number variable, below the bracket Commented Jul 10, 2019 at 7:35
  • What is the error message? When you hover your mouse over the red line, what does it say? Commented Jul 10, 2019 at 7:38
  • Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement Commented Jul 10, 2019 at 7:41
  • double number = ""; causes an error. double number = 0; will do the (guessed) expected. And number; will surely cause a compiler error. not sure which result you expect here. Commented Jul 10, 2019 at 12:49

1 Answer 1

1

I have two findings here:

number += rate_num;

I guess you want a sum of the rates. Since number is defined as a string you will get a string as result where all rates are concatenated. Define number as double:

double number = 0;

Second is the line after the while-loop:

number;

What do you want to archive here? This line will not compile neither if number is a string nor double. If this code is part of a method you pssibly want to return the calculated sum of rates. So you could

return number;

But first close your DB connection

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.