Fetching SQL Query Result DataRow by Row into RichTextBox/TextBox

We can fetch SQL query result set data in RichTextBox on the row by row basis.

Now suppose there is a table "DB_Employee" having columns EmpID, EmpName, EmpDepartment, EmpAddress in database "TestDB". So we have to fetch available rows on a button1 click event into RichTextBox1.

  1. private void bitton1_click(object sender, EventArgs e) {  
  2.   
  3.     try {  
  4.   
  5.         SqlConnection con = new SqlConnection("server=localhost; database=TestDB; integrated security=sspi;");  
  6.   
  7.         SqlCommand cmd = new SqlCommand("Select * From DB_Test", con);  
  8.   
  9.         con.Open();  
  10.   
  11.         using(SqlDataReader dr = cmd1.ExecuteReader()) {  
  12.   
  13.             While(dr.Read()) {  
  14.                 //Append the rows data  
  15.                 RichTextBox1(dr.AppendText(dr.GetValue(0).ToString()));  
  16.                 RichTextBox1(dr.AppendText(dr.GetValue(1).ToString()));  
  17.                 RichTextBox1(dr.AppendText(dr.GetValue(2).ToString()));  
  18.                 RichTextBox1(dr.AppendText(dr.GetValue(3).ToString()));  
  19.   
  20.                 //Change the line  
  21.                 RichTextBox1 += system.Environment.NewLine;  
  22.             }  
  23.         }  
  24.     } catch (Exception ex) {  
  25.         MessageBox.Show(ex.Message);  
  26.     } finally {  
  27.         con.Close();  
  28.     }  
  29. }