-2

I'm displaying some data from the database into textboxes on my windows form and I have to add something else but it depends on the account type (that information is saved on my Databse). Meaning it is either DM (domestic) or CM(comercial). I want for it to charge an extra amount if it's CM. I would appreciate any help, thank you.

OracleDataReader myReader = null;
OracleCommand myCommand = new OracleCommand("SELECT SUM(IMPORTE) AS corriente FROM MOVIMIENTOS WHERE CUENTA='"+txtIngreseCuenta3.Text + "'AND CONCEPTO=10", connectionString);

myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
    txtCorriente.Text = (myReader["corriente"].ToString());
}

I'm using this code to display into the textbox but I want it to get the account type from another table and IF it's CM then add a certain amount into a textbox.

Mozart
  • 2,117
  • 2
  • 20
  • 38
Christine
  • 17
  • 5
  • What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! - Also: Please fix the formatting! – TaW Aug 13 '19 at 20:43
  • Please read [Why do we always prefer using parameters in SQL statements?](https://stackoverflow.com/questions/7505808/). – Dour High Arch Aug 13 '19 at 20:50

1 Answers1

2

In your case I suggest that you use Execute Scalar instead of reader since you are returning only one value from the database. but in your case if you want this code to work you need to Cast the returned value into the correct dot net type and then populate the TextBox.

Example Using ExecuteReader

//txtCorriente.Text = ((int)myReader["corriente"]).ToString();

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        connection.Open();

        SqlCommand command = new SqlCommand(queryString, connection);
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            txtCorriente.Text = ((int)reader["corriente"]).ToString();
            txtAnother.Text = ((decimal)reader["another"]).ToString();

            Console.WriteLine(String.Format("{0}", reader[0]));
        }
    }
}

Example Using ExecuteScalar

static public int AddProductCategory(string newName, string connString)
{
    Int32 newProdID = 0;
    string sql =
        "INSERT INTO Production.ProductCategory (Name) VALUES (@Name); "
        + "SELECT CAST(scope_identity() AS int)";
    using (SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@Name", SqlDbType.VarChar);
        cmd.Parameters["@name"].Value = newName;
        try
        {
            conn.Open();
            newProdID = (Int32)cmd.ExecuteScalar();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    return (int)newProdID;
}
Mozart
  • 2,117
  • 2
  • 20
  • 38
  • 1
    More like `txtCorriente.Text = ((decimal)mycommand.ExecuteScalar).ToString();` We really don't know the datatype of the IMPORTE column so just guessing decimal. – Mary Sep 02 '19 at 03:43