Actually i am reading a table from data base through data reader and displaying it in the gridview and i am doing well the problem i want to read and display 2 tables at a time in the same grid view.
Here is my Code in the class DBHandler.
Expand|Select|Wrap|Line Numbers
- public class DBHandler
- {
- private const String ConnectionString = @"initial catalog=FinalProjectDB;Data Source=ALIEN\SQLEXPRESS;integrated Security=true;";
- public List<Programs> GetAllPrograms()//Programs is the class name
- {
- SqlConnection con = new SqlConnection(ConnectionString);
- SqlCommand com = new SqlCommand();
- com.CommandType = System.Data.CommandType.Text;
- com.CommandText = "SELECT * FROM Programs";
- com.Connection = con;
- List<Programs> Program = new List<Programs>();//Programs is the class name
- SqlDataReader reader;
- con.Open();
- reader = com.ExecuteReader();
- if (reader.HasRows)
- {
- while (reader.Read())
- {
- Programs P = new Programs();//Object Created from class Programs
- P.Id = Convert.ToInt32(reader["ProgramID"]);//Reading the contents of Table in database using datareader and assigning them to class programs object
- P.Title = reader["Title"].ToString();
- P.Instructor = reader["Instructor"].ToString();
- Program.Add(P);
- }
- }
- con.Close();
- return Program;
- }
And the code in my form is.
Expand|Select|Wrap|Line Numbers
- private void Form1_Load(object sender, EventArgs e)
- {
- DBHandler ObjDBHandler = new DBHandler();
- //dataGridView3.AutoGenerateColumns = false;
- dataGridView3.DataSource = ObjDBHandler.GetAllPrograms();
- }
Please help me as soon as possible.
Thankyou.
}