Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: m@o_614 en 14 Mayo 2013, 03:06 am



Título: cast tipo datePicker a string
Publicado por: m@o_614 en 14 Mayo 2013, 03:06 am
saludos tengo este codigo que me esta dando un error en la parte de buscar, que me tiene que buscar un numero de cuenta y si lo encuentra mostrarme todos los demas datos de la entidad, me dice que no se puede convertir de datePicker a string

Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using Npgsql;
  10.  
  11. namespace Agencia_de_Viajes
  12. {
  13.    public partial class Form5 : Form
  14.    {
  15.        public Form5()
  16.        {
  17.            InitializeComponent();
  18.        }
  19.  
  20.        private void irMenu_Click(object sender, EventArgs e)
  21.        {
  22.            this.Visible = false;
  23.            Form1 menu = new Form1();
  24.            menu.Visible = true;
  25.        }
  26.  
  27.        private void cmdAgregar_Click(object sender, EventArgs e)
  28.        {
  29.            if (txtNumeroCuenta.Text == "" || txtDescripcion.Text == "" || txtIdCliente.Text == "")
  30.            {
  31.                MessageBox.Show("DEBES INTRODUCIR VALORES...");
  32.            }          
  33.            else
  34.            {
  35.                int NUMERO = Convert.ToInt16(txtNumeroCuenta.Text);
  36.                string DESCRIPCION = txtDescripcion.Text;
  37.                int ID = Convert.ToInt16(txtIdCliente.Text);
  38.                string FECHA = dateTimePicker1.Value.ToString("yyyy-MM-dd");
  39.  
  40.                try
  41.                {
  42.                    IDbConnection dbcon = new NpgsqlConnection("Server = localhost;" + "Database = Facturas;" + "User ID = marcela;");
  43.                    dbcon.Open();
  44.                    IDbCommand dbcmd = dbcon.CreateCommand();
  45.                    dbcmd.CommandText = "insert into Cuentas_extras values(" + NUMERO + ",'" + DESCRIPCION + "'," + ID + ",'" + FECHA + "');";
  46.                    IDataReader reader = dbcmd.ExecuteReader();
  47.                    dbcon.Close();
  48.                    txtNumeroCuenta.Text = "";
  49.                    txtDescripcion.Text = "";
  50.                    txtIdCliente.Text = "";
  51.                    MessageBox.Show("Registro Guardado correctamente");
  52.                }
  53.                catch (Exception msg)
  54.                {
  55.                    MessageBox.Show("error.....\n\n" + msg.ToString());
  56.                }
  57.            }
  58.        }
  59.  
  60.        private void cmdMostrar_Click(object sender, EventArgs e)
  61.        {
  62.            try
  63.            {
  64.                NpgsqlConnection conexion = new NpgsqlConnection("Server = localhost; " + "Database = Facturas;" + "User ID = marcela;");
  65.  
  66.                NpgsqlDataAdapter adaptador = new NpgsqlDataAdapter("select * from Cuentas_extras", conexion);
  67.                DataTable tablamemoria = new DataTable();
  68.                adaptador.Fill(tablamemoria);
  69.                dtaCuentasExtras.DataSource = tablamemoria.DefaultView;
  70.            }
  71.  
  72.            catch (Exception msg)
  73.            {
  74.                MessageBox.Show("Error......\n\n" + msg.ToString());
  75.            }
  76.        }
  77.  
  78.        int n;
  79.        private void cmdBuscar_Click(object sender, EventArgs e)
  80.        {
  81.            if (txtBuscado.Text == "")
  82.            {
  83.                MessageBox.Show("DEBES INTRODUCIR EL NUMERO DE CUENTA A BUSCAR...");
  84.                txtBuscado.Focus();
  85.            }
  86.            else
  87.            {
  88.                try
  89.                {
  90.                    int band = 0;
  91.                    IDbConnection dbcon = new NpgsqlConnection("Server = localhost;" + "Database = Facturas;" + "User ID = marcela;");
  92.                    dbcon.Open();
  93.                    IDbCommand dbcmd = dbcon.CreateCommand();
  94.                    n = Convert.ToInt32(txtBuscado.Text);
  95.                    dbcmd.CommandText = "select * from cuentas_extras where numero_cuenta =" + n + "";
  96.                    IDataReader reader = dbcmd.ExecuteReader();
  97.                    if (reader.Read())
  98.                    {
  99.                        txtDescripcion.Text = reader.GetString(reader.GetOrdinal("descripcion"));
  100.                        txtIdCliente.Text = Convert.ToString(reader.GetInt32(reader.GetOrdinal("id_cliente")));
  101.                        txtNumeroCuenta.Text = Convert.ToString(reader.GetInt32(reader.GetOrdinal("numero_cuenta")));
  102.                        dateTimePicker1.Text = reader.GetString(reader.GetOrdinal("fecha"));
  103.                        band = 1;
  104.                        txtBuscado.Text = "";
  105.                    }
  106.                    dbcon.Close();
  107.                    if (band == 0)
  108.                    {
  109.                        txtBuscado.Text = "";
  110.                        txtDescripcion.Text = "";
  111.                        txtIdCliente.Text = "";
  112.                        txtNumeroCuenta.Text = "";
  113.                        MessageBox.Show("Numero de cuenta no encontrado");
  114.                    }
  115.                }
  116.                catch (Exception msg)
  117.                {
  118.                    MessageBox.Show(msg.ToString());
  119.                }
  120.            }
  121.        }
  122.    }
  123. }
  124.  

gracias