473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Reading multiple tables from database through datareader

Hi dear fellows...i am a newbie in C# and practicing it hardly to learn it.

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
  1. public class DBHandler
  2. {
  3.  
  4. private const String ConnectionString = @"initial catalog=FinalProjectDB;Data Source=ALIEN\SQLEXPRESS;integrated Security=true;";
  5.  
  6. public List<Programs> GetAllPrograms()//Programs is the class name
  7.  
  8. {
  9.  
  10. SqlConnection con = new SqlConnection(ConnectionString);
  11. SqlCommand com = new SqlCommand();
  12. com.CommandType = System.Data.CommandType.Text;
  13. com.CommandText = "SELECT * FROM Programs";
  14. com.Connection = con;
  15.  
  16. List<Programs> Program = new List<Programs>();//Programs is the class name
  17.  
  18. SqlDataReader reader;
  19.  
  20. con.Open();
  21.  
  22. reader = com.ExecuteReader();
  23.  
  24. if (reader.HasRows)
  25. {
  26. while (reader.Read())
  27. {
  28. Programs P = new Programs();//Object Created from class Programs
  29.  
  30. P.Id = Convert.ToInt32(reader["ProgramID"]);//Reading the contents of Table in database using datareader and assigning them to class programs object
  31. P.Title = reader["Title"].ToString();
  32. P.Instructor = reader["Instructor"].ToString();
  33.  
  34. Program.Add(P);
  35. }
  36.  
  37. }
  38.  
  39. con.Close();
  40.  
  41. return Program;
  42.  
  43. }
I am using a class DB handler to read data from Data base.

And the code in my form is.

Expand|Select|Wrap|Line Numbers
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3.  
  4. DBHandler ObjDBHandler = new DBHandler();
  5.  
  6. //dataGridView3.AutoGenerateColumns = false;
  7.  
  8. dataGridView3.DataSource = ObjDBHandler.GetAllPrograms();
  9.  
  10. }
There are total two class are involved one is DBHandler Which i explained above and another class Programs having information like ID, Title etc of the study programs

Please help me as soon as possible.

Thankyou.

}
Sep 8 '09 #1
1 9278
Are these tables related to each other?
Gridview can show values from Dataset. So if values are related then using JOIN's can show the table to the Grid View.
Sep 10 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Top Gun | last post by:
In order to avoid multiple trips to the database, I would like to fill several tables in a DataSet with a single call to a stored procedure that will return resultsets for the appropriate tables. ...
4
by: Michael Carr | last post by:
I have a function that populates a class with values from a database. I'd like to pass into the function either a SqlDataReader or a DataRow, depending on which mechanism I'm using to retrieve data...
3
by: Yul | last post by:
Hi, We are in the process of designing an ASP.NET app, where a user will enter some 'Customer ID' to be queried in the database. If the ID is valid, several stored procedures will be called to...
1
by: Justin | last post by:
What is the best/easiest way to update multiple tables at a time? Can you recommend any tutorials? Thanks, Justin.
3
by: Joe via DotNetMonster.com | last post by:
Hi, I'm trying to use several select statements so that I don't need to call the function several times. The next Result set always seems to read the first select statement. I have the...
8
by: Tull Clancey | last post by:
Hi, hope someone can help. I'm writing a decent size app as a first project in Net, have been writing in VB for years. I've stumbled on a small problem, I can't open two connections to a...
6
by: Baal80 | last post by:
Hello, I've managed to read an sql type from the database (using a kind of virtual commandbuilder and SqlDbType method) but I'm not able to retrieve exact data type (eg. maximum length of a...
9
by: Blarneystone | last post by:
Hi, I am using VB.NET and trying to pull data from two different tables in the database. I am using what I think is standard code. But the data I am pulling is like the following: Table1...
1
by: jinxy | last post by:
Hello, this is my code in visual c#: namespace im_client { public partial class Form2 : Form { MySqlConnection conDatabase = new MySqlConnection("Data Source=86.123.220.28;" +...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.