Ayuda en C#

Grunt

Bovino adicto
#1
Hola compañeros vacunos, pues en esta ocasion vengo a pedirles su ayuda para un programa en C# conectado con SQL.

Ya tengo los botones de grabar y eliminar y me salen bien, el problema es con consultar y editar, hai es donde quisiera que me ayuden a corregir mi codigo.

Esto es lo que llevo:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication2
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}

privatevoid button1_Click(object sender, EventArgs e)
{
String SQLINSERT = "INSERT INTO Clientes VALUES (@Id, @Nombre, @Telefono, @Saldo)";
SqlCommand Comando = newSqlCommand (SQLINSERT , Conexion);
Comando.CommandType = CommandType.Text;
Comando.Parameters.AddWithValue ("@Id", Id.Text);
Comando.Parameters.AddWithValue ("@Nombre", Nombre.Text);
Comando.Parameters.AddWithValue ("@Telefono", Telefono.Text);
Comando.Parameters.AddWithValue ("@Saldo", Saldo.Text);
Conexion.Open();
Comando.ExecuteNonQuery();
Conexion.Close();
MessageBox.Show("El Registro Fue Almacenado");

Id.Clear();
Id.Focus();
}
privatevoid button2_Click(object sender, EventArgs e)
{
string strSQL = "SELECT * FROM Datos WHERE Id=" + Id.Text + ";";
SqlCommand Comando = newSqlCommand(strSQL, Conexion);
Comando.CommandType = CommandType.Text;
Conexion.Open();
SqlDataReader Read = Comando.ExecuteReader(); /// esta linea no queda con los reads
Read.Read();
if (Read.HasRows == true)
{
Nombre.Text = Read[1].ToString();
Telefono.Text = Read[2].ToString();
Saldo.Text = Read[3].ToString();
}
Conexion.Close();

}
privatevoid button3_Click(object sender, EventArgs e)
{
String SQLINSERT = ("DELETE FROM Clientes Where id=@Id");
SqlCommand Comando = newSqlCommand(SQLINSERT, Conexion);
Comando.CommandType = CommandType.Text;
Comando.Parameters.AddWithValue("@Id", Id.Text);
Conexion.Open();
Comando.ExecuteNonQuery();
Conexion.Close();
Id.Clear();
Id.Focus();
MessageBox.Show("El Contacto Fue Eliminado", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);


}
privatevoid button4_Click(object sender, EventArgs e)
{
String SQLINSERT = ("UPDATE Clientes Set Id=@Id, Nombre=@Nombre, Telefono=@Telefono, Saldo=@Saldo WHERE Id=@Id");
SqlCommand Comando = newSqlCommand (SQLINSERT , Conexion);
Comando.CommandType = CommandType.Text;
Comando.Parameters.AddWithValue ("@Id", Id.Text);
Comando.Parameters.AddWithValue ("@Nombre", Nombre.Text);
Comando.Parameters.AddWithValue ("@Telefono", Telefono.Text);
Comando.Parameters.AddWithValue ("@Saldo", Saldo.Text);
Conexion.Open();
Comando.ExecuteNonQuery();
Conexion.Close();
Id.Clear();
Id.Focus();
MessageBox.Show("El Datos del Contacto Fueron Actualizados", "Actualizar", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
}
}


P:eek: La tabla se llama Clientes y mis subtablas son: Id, Nombre, Telefono y Saldo.

Cualquier ayuda se los agadeceria mucho :vientos:.
 

Grunt

Bovino adicto
#4
por que no usas un dataset o un datatable para la consulta de informacion...
Tengo poco programando y 1 semana con C#, no se que es eso :(.



donde tienes declarado el parametro Conexion?,....no lo encuentro
Desde el cuadro de Herramientas le agregue un SqlConection a el Form y pues le puse Conexion y pues con eso ya se conecta a mis Tablas, bueno, con eso los botones de grabar y eliminar si sirven.
 
#5
No estoy muy al tiro en C#, pero donde tienes:

Código:
[SIZE=2][COLOR=#0000ff]string[/COLOR] strSQL = [COLOR=#a31515][COLOR=#a31515]"SELECT * FROM Datos WHERE Id="[/COLOR][/COLOR] + Id.Text + [COLOR=#a31515][COLOR=#a31515]";"[/COLOR][/COLOR];[/SIZE]
Ponle

Código:
[SIZE=2][COLOR=#0000ff]string[/COLOR] strSQL = [COLOR=#a31515][COLOR=#a31515]"SELECT * FROM Datos WHERE Id= "[/COLOR][/COLOR] + Id.Text.trim() ;[/SIZE]
En cuando al UPDATE

Código:
[SIZE=2]Comando.Parameters.AddWithValue ([COLOR=#a31515][COLOR=#a31515]"@Id"[/COLOR][/COLOR], Id.Text);[/SIZE]
Cambiale a:

Código:
[SIZE=2]Comando.Parameters.AddWithValue ([COLOR=#a31515][COLOR=#a31515]"@Id"[/COLOR][/COLOR], Id.Text.trim());[/SIZE]
 

Grunt

Bovino adicto
#6
No estoy muy al tiro en C#, pero donde tienes:

Código:
[SIZE=2][COLOR=#0000ff]string[/COLOR] strSQL = [COLOR=#a31515][COLOR=#a31515]"SELECT * FROM Datos WHERE Id="[/COLOR][/COLOR] + Id.Text + [COLOR=#a31515][COLOR=#a31515]";"[/COLOR][/COLOR];[/SIZE]
Ponle

Código:
[SIZE=2][COLOR=#0000ff]string[/COLOR] strSQL = [COLOR=#a31515][COLOR=#a31515]"SELECT * FROM Datos WHERE Id= "[/COLOR][/COLOR] + Id.Text.trim() ;[/SIZE]
En cuando al UPDATE

Código:
[SIZE=2]Comando.Parameters.AddWithValue ([COLOR=#a31515][COLOR=#a31515]"@Id"[/COLOR][/COLOR], Id.Text);[/SIZE]
Cambiale a:

Código:
[SIZE=2]Comando.Parameters.AddWithValue ([COLOR=#a31515][COLOR=#a31515]"@Id"[/COLOR][/COLOR], Id.Text.trim());[/SIZE]

Hola, comento que me marco error al momento de agregar los .trim() al codigo.

Saludos.
 

burn2crashu2

Bovino adolescente
#9
Quiubole amigo,a ver checa esto

El problema al hacer el Select es q el nombre de la tabla esta mal, en vez de
string strSQL = "SELECT * FROM Datos WHERE Id=" + Id.Text + ";";
ponle
string strSQL = "SELECT * FROM Clientes WHERE Id=" + Id.Text;
bueno asi quedaria mejor:
privatevoid button2_Click(object sender, EventArgs e)
{
string strSQL = "SELECT * FROM Clientes WHERE Id=@Id";
SqlCommand Comando = newSqlCommand(strSQL, Conexion);
Comando.CommandType = CommandType.Text;
Comando.Parameters.AddWithValue ("@Id", Id.Text);
Conexion.Open();
SqlDataReader Read = Comando.ExecuteReader(); /// esta linea no queda con los reads
Read.Read();
if (Read.HasRows == true)
{
Nombre.Text = Read[1].ToString();
Telefono.Text = Read[2].ToString();
Saldo.Text = Read[3].ToString();
}
Conexion.Close();

}


Y para lo del Update, hazle asi:
privatevoid button4_Click(object sender, EventArgs e)
{
String SQLINSERT = ("UPDATE Clientes Set /*Id=@Id,*/ Nombre=@Nombre, Telefono=@Telefono, Saldo=@Saldo WHERE Id=@Id");
SqlCommand
Comando = newSqlCommand (SQLINSERT , Conexion);
Comando.CommandType = CommandType.Text;
Comando.Parameters.AddWithValue ("@Id", Id.Text);
Comando.Parameters.AddWithValue ("@Nombre", Nombre.Text);
Comando.Parameters.AddWithValue ("@Telefono", Telefono.Text);
Comando.Parameters.AddWithValue ("@Saldo", Saldo.Text);
Conexion.Open();
Comando.ExecuteNonQuery();
Conexion.Close();
Id.Clear();
Id.Focus();
MessageBox.Show("Los Datos del Contacto Fueron Actualizados", "Actualizar", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
checa q ya no le estoy asignando un valor al campo Id, si lo definiste como llave primaria entonces es la razon por la q no puedes hacer el update. Te recomiendo q no modifiques las llaves primarias.
Si te siguen saliendo mensajes de error entonces si quieres hazles paste aqui

Bueno ahi luego nos dices q chou ;)
 

Grunt

Bovino adicto
#10
Hola a todos, les agradesco mucho sus respuestas.

Saben donde estaba el problema?

privatevoid button2_Click(object sender, EventArgs e)
{
string strSQL = "SELECT * FROM Datos WHERE Id=" + Id.Text + ";";
SqlCommand Comando = newSqlCommand(strSQL, Conexion);
Comando.CommandType = CommandType.Text;
Conexion.Open();
SqlDataReader Read = Comando.ExecuteReader(); /// esta linea no queda con los reads
Read.Read();
if (Read.HasRows == true)
{
Nombre.Text = Read[1].ToString();
Telefono.Text = Read[2].ToString();
Saldo.Text = Read[3].ToString();
}
Conexion.Close();

}

En Datos... ya que no es Datos, esa era una tabla de prueba, la verdadera tabla es Clientes.

Me pueden hacer asi :tomate: :(.

Muchas gracias compañeros Bakunos :), aun no termino el programa, si se me traba en algo espero poder venir nuevamente a invocar su ayuda.

Saludos y denuevo gracias :aplausos:.
 
Arriba