I am completely new to programming and I am trying to create a small app as a school project. I want it to be able to register and login users. I have already figured out how to create login part, but I am stuck on registration. I have created Insert function inside pgAdmin and it works but I cant make it work with my windows forms app.
This is my code so far:
using Npgsql;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjektV0._0._2
{
public partial class frmRegister : Form
{
public frmRegister()
{
InitializeComponent();
}
private NpgsqlConnection conn;
string connstring = String.Format("Server={0}; Port={1};" +
"User Id = {2}; Password={3};Database={4};",
"localhost", "5432", "postgres", "23112001", "demo2");
private NpgsqlCommand cmd;
private string sql = null;
private void frmRegister_Load(object sender, EventArgs e)
{
conn = new NpgsqlConnection(connstring);
}
private void Register_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void btnRegister_Click(object sender, EventArgs e)
{
try
{
conn.Open();
sql = @"select * from u_insert(:_username,:_password)";
cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue("_username", txtEmail.Text);
cmd.Parameters.AddWithValue("_password", txtPswrd.Text);
if ((int)cmd.ExecuteScalar() == 1)
{
conn.Close();
MessageBox.Show("Registered successfuly", "Well done", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
txtEmail.Text = txtPswrd.Text = txtConPswrd.Text = null;
}
}
catch (Exception ex)
{
conn.Close();
MessageBox.Show("Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
pgAdmin part:
create function u_insert
(
_username character varying,
_password character varying
)returns int as
$$
begin
insert into tbl_users
(
username,
password
)values
(
_username,
_password
);
if found then
return 1;-----success-----
else
return 0;-----fail-----
end if;
end
$$
language plpgsql
As I said my login part work even through my program and all other functions (insert,update) work only inside pgAdmin.
if found thenwhere did it come from? Does the function even work?select * from u_insert('x','y')) inside pgAdmin it does work.