1

I'm using PostgreSQL DB, and I want to make a data displayer for my friend who doesn't know SQL but sometimes he need to get some information.

I want to write a Windows Forms application which should allow him to type some client id and in result give him some data about this client.

I wrote something like this (below) and I am stuck - after clicking a button I want to pass the answer of my query to a textBox and I can't, tried many solutions. None worked. Anyone can help ?

    private void btShowData_Click(object sender, EventArgs e)
    {
        NpgsqlConnection conn = new NpgsqlConnection("Server=someServer;Port=1234;User ID=some_user;Password=123456;Database=someDataBase;");
        conn.Open();

        NpgsqlCommand answer1 = new NpgsqlCommand("SELECT column1 FROM table1 WHERE id = :userId", conn);
        answer1.Parameters.Add(new NpgsqlParameter("userId", NpgsqlTypes.NpgsqlDbType.Integer));
        answer1.Parameters[0].Value = tbId.Text;
   // HERE IS MY PROBLEM - How to pass the answer1 result to the textbox
    }

3 Answers 3

1

I'm not too much familiar with PostgresSQL but I think you can use ExecuteScalar method. It returns first column of the first row as an object.

If your column1 is character type, you can use it like;

...
answer1.Parameters[0].Value = tbId.Text;
string s = (string)answer1.ExecuteScalar();
TextBox1.Text = s;
Sign up to request clarification or add additional context in comments.

Comments

1

Solution 1:

NpgsqlDataReader reader = answer1.ExecuteReader();
if(reader.Read())
{
    yourTextBox.Text = reader["column1"].ToString();
}

Solution 2:

yourTextBox.Text = answer1.ExecuteScalar().ToString();

Comments

-1

To pass text to a TextBox all you need to do is write:

TextBoxName.Text = yourVariable.ToString();

2 Comments

What is yourVariable? o.O
Sorry for beeing unclear, answer1.ToString() adds the variables value you your textbox. Using the above mentioned method ExecuteScalar().Tostring() does what you are asking. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.