Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: m@o_614 en 14 Abril 2013, 21:28 pm



Título: mensaje de error c#
Publicado por: m@o_614 en 14 Abril 2013, 21:28 pm
Saludos

estoy haciendo el siguiente código  en c# pero me marca dos errores en varias lineas del código que dicen

El nombre txtId no existe en el conexto actual
El nombre txtNombre no existe en el contexto actual

no entiendo por qué me da esos errores, esta es la primera vez que programo en c#

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. using Mono.Security;
  11.  
  12. namespace Proyecto_BasesDatos
  13. {
  14.    public partial class Form1 : Form
  15.    {
  16.        public Form1()
  17.        {
  18.            InitializeComponent();
  19.        }
  20.  
  21.        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  22.        {
  23.  
  24.        }
  25.  
  26.        private void cmdAgregar_Click(object sender, EventArgs e)
  27.        {
  28.            if (txtId.Text == "" || txtNombre.Text == "")
  29.            {
  30.                MessageBox.Show("Debes Introducir Valores");
  31.            }
  32.            else
  33.            {
  34.                try
  35.                {
  36.                    string Nombre = txtNombre.Text;
  37.                    int Id = Convert.ToInt16(txtId.Text);
  38.                    IDbConnection dbcon = new NpgsqlConnection("Server=localhost;" + "Database=Conexion;" + "User Id=Marcela;");
  39.                    dbcon.Open();
  40.                    IDbCommand dbcmd = dbcon.CreateCommand();
  41.                    dbcmd.CommandText = "insert into Clientes values (" + Id + " , '" + Nombre + "')";
  42.                    IDataReader reader = dbcmd.ExecuteReader();
  43.                    dbcon.Close();
  44.                    txtNombre.Text = "";
  45.                    txtId.Text = "";
  46.                    MessageBox.Show("Registro Guardado Correctamente");
  47.                }
  48.                catch (Exception msg)
  49.                {
  50.                    MessageBox.Show("error.......................\n\n" + msg.ToString());
  51.                }
  52.  
  53.            }
  54.  
  55.        }
  56.  
  57.  
  58.    }
  59. }

gracias


Título: Re: mensaje de error c#
Publicado por: Eleкtro en 16 Abril 2013, 09:05 am
El nombre txtId no existe en el conexto actual
El nombre txtNombre no existe en el contexto actual

no entiendo por qué me da esos errores, esta es la primera vez que programo en c#

Código:
 if (txtId.Text == "" || txtNombre.Text == "")

Eso equivale a:
Código:
if (Objeto1.PropiedadText = vacío || Objeto2.PropiedadText = vacío)

¿Lo vas pillando :P?, en el formulário falta que agreges un control que tenga la propiedad "Text" (Por ejemplo un Textbox) con el nombre "txtId", y luego agregas otro control (otro textbox) y le cambias el nombre a: "txtNombre", puedes agregar los controles en el código o arrastrándolos desde el Designer.

Saludos!