Monday, February 7, 2011

Populate Datagrid Using Datareader in C#.Net

try
{
string SQLstr = "Select * from Employee";

SqlConnection con = new SqlConnection("Connection_String");

//Open Connection
con.Open();

SqlCommand com = new SqlCommand(SQLstr, con);
SqlDataReader read = com.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(read);

// finally bind the data to the grid
dataGridView1.DataSource = dt;

// Close Connection
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error");
}

No comments:

Post a Comment