vikas m

vikas m

  • NA
  • 3
  • 1.1k

code to fetch data from database and display all records

Jul 31 2014 11:02 AM
hi,
 
i have written a code to fetch data from database and display it in a textbox
but the problem is that it displays only the last value
what modification should i do to display all values in the textbox one by one
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace sample2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
db();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void db()
{
SqlConnection con = new SqlConnection("Data Source=xxxx;Initial Catalog=temp1;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("Select * from t1",con);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
textBox1.Text = reader.GetValue(0).ToString();
}
con.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
 

Answers (3)