I'm creating a client-server chat application using WCF. To connect I use the same code as for my other app, but it doesn't work here. This is part of my Code
Client:
private void bReg_Click(object sender, RoutedEventArgs e)
{
client = new ServiceChatClient(new System.ServiceModel.InstanceContext(this));
string response = client.Registration(tbUserName.Text,tbPassword.Text);
MessageBox.Show(response);
string[] responseSub = response.Split('#');
if (responseSub[1] == string.Empty)
{
MessageBox.Show(responseSub[0]);
}
else
{
MessageBox.Show(responseSub[0]);
}
}
ServiceChat:
string connstring = String.Format("Server={0};Port={1};" + "User ID = {2};Password={3};Database={4};", "localhost", "5432", "postgres", "mypass", "users");
public NpgsqlCommand cmd;
public string sql = null;
public string Registration(string username, string password)
{
try
{
NpgsqlConnection conn = new NpgsqlConnection(connstring);
conn.Open();
sql = @"SELECT * FROM users where username = :username";
NpgsqlCommand cmd = new NpgsqlCommand(sql, conn);
cmd.Parameters.AddWithValue(":username", username);
int result = Convert.ToInt32(cmd.ExecuteScalar());
if (result == 0)
{
sql = ("INSERT INTO USERS (username,password) VALUES (:username,:password)");
cmd = new NpgsqlCommand(sql, conn);
if (password.Length >= 4 && username.Length > 0 && password.Length <= 128 &&
username.Length <= 128 && !password.Contains(' '))
{
cmd.Parameters.AddWithValue("username", username);
cmd.Parameters.AddWithValue("password", GetMD5(password));
cmd.ExecuteNonQuery();
conn.Close();
return "Registration completed successfully#OK";
}
else
{
conn.Close();
return "Invalid username or password#";
}
}
else
{
conn.Close();
return "A user with the same username already exists#";
}
}
catch (Exception ex)
{
string error = string.Format("Error:{0} что-то пошло не так#", ex);
return error;
}
}
I tried clearing the NpgsqlConnection pool but it didn't help.I just started working with WCF, so there may be a mistake somewhere else