1
I’m having a problem understanding how to return the values within windows form (fill the textbox, etc).
This method executes the Reader data but I don’t know how I can use it to fill in the fields.
public bool BuscarCliente(int id)
{
using (conn = new NpgsqlConnection(ConnString))
{
conn.Open();
string cmdBuscar = String.Format("SELECT * FROM CLIENTES WHERE ID=@ID");
using (NpgsqlCommand cmd = new NpgsqlCommand(cmdBuscar, conn))
{
cmd.Parameters.Add(new NpgsqlParameter("@ID", id));
NpgsqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
//campos
}
else
{
//erro
}
}
}
}
I saw in some topics the staff uses this way:
Example: txtBairro.Text = reader[0].ToString();
But in this case he used inside the button and in my case is in a class.
How do I do that?
Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
using System.Collections;
namespace GE
{
class FDP
{
static string serverName = "localhost";
static string port = "5432";
static string userName = "postgres";
static string password = "adm";
static string databaseName = "GE";
NpgsqlConnection conn = null;
string ConnString = null;
public FDP()
{
ConnString = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
serverName, port, userName, password, databaseName);
}
public bool InserirCliente(ArrayList p_arr)
{
using (conn = new NpgsqlConnection(ConnString))
{
conn.Open();
string cmdInserir = String.Format("INSERT INTO CLIENTES (NOME, ENDERECO, CIDADE, EMAIL, TELEFONE) VALUES (@NOME, @ENDERECO, @CIDADE, @EMAIL, @TELEFONE)");
try
{
using (NpgsqlCommand cmd = new NpgsqlCommand(cmdInserir, conn))
{
cmd.Parameters.Add(new NpgsqlParameter("@NOME", p_arr[0]));
cmd.Parameters.Add(new NpgsqlParameter("@ENDERECO", p_arr[1]));
cmd.Parameters.Add(new NpgsqlParameter("@CIDADE", p_arr[2]));
cmd.Parameters.Add(new NpgsqlParameter("@EMAIL", p_arr[3]));
cmd.Parameters.Add(new NpgsqlParameter("@TELEFONE", p_arr[4]));
cmd.ExecuteNonQuery();
return true;
}
}catch (NpgsqlException ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
}
public int UltimoRegistroCliente()
{
using (conn = new NpgsqlConnection(ConnString))
{
conn.Open();
string cmdLast = String.Format("SELECT * FROM CLIENTES ORDER BY ID_CLIENTE DESC LIMIT 1;");
using (NpgsqlCommand cmd = new NpgsqlCommand(cmdLast, conn))
{
return Convert.ToInt32(cmd.ExecuteScalar());
}
}
}
public bool BuscarCliente(int id)
{
using (conn = new NpgsqlConnection(ConnString))
{
conn.Open();
string cmdBuscar = String.Format("SELECT * FROM CLIENTES WHERE ID=@ID");
using (NpgsqlCommand cmd = new NpgsqlCommand(cmdBuscar, conn))
{
cmd.Parameters.Add(new NpgsqlParameter("@ID", id));
NpgsqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
//campos
}
else
{
//erro
}
}
}
}
public bool DeletarCliente(int id)
{
using (conn = new NpgsqlConnection(ConnString))
{
conn.Open();
string cmdDeletar = String.Format("DELETE FROM CLIENTES WHERE ID_CLIENTE=@ID");
using (NpgsqlCommand cmd = new NpgsqlCommand(cmdDeletar, conn))
{
cmd.Parameters.Add(new NpgsqlParameter("@ID", id));
cmd.ExecuteNonQuery();
return true;
}
}
}
}
}
Create properties in your class and pass the values to it, in your form you create an instance of your class and call the method
BuscarCliente
, after that you take the content of its properties and play for the textbox– Roberto de Campos
@Robertodecampos guy did not understand very well, if you can answer in a more practical way, Gradeco
– WSS