Ender Bir Adam Ender Bir Adam - 2 years ago 150
SQL Question

C# - Selecting SQL Table contents into textbox

Trying to get table contents into textBoxes seperately by the prefered ID number when button_click event occours. I've found some videos about the situation but dataGrids were used in those. I'm a complete newbie in C# btw, and i'm not even sure what i'm doing.

Question is, i cant figure how to write a read-only object that is stated below in code into a textBox. I'm %90 sure that it's truly a simple thing, and i'm missing it.

private void button2_Click(object sender, EventArgs e)
SqlCommand rcmd = new SqlCommand("SELECT ID, Column1, Column2 FROM [TEST].[dbo].[Table_1] where ID=@ID", connection);
rcmd.Parameters.AddWithValue("@ID", textBox3.Text);
SqlDataReader reader = rcmd.ExecuteReader();

while (reader.Read())
//textBox4.Text = reader["Column1"];
//textBox5.Text = reader["Column2"];
//These wont work, says reader["Column1"] and reader["Column2"] are read-only. What to do?

Lines with error are intentionally commented. No mistake there. I was trying to figure something else while keeping those in my sight.

Thanks in advance.

Answer Source

You're missing ToString()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download