6 Replies - 12322 Views - Last Post: 17 January 2009 - 10:58 PM Rate Topic: -----

#1 afridi2129   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Reading Multiple records from database

Post icon  Posted 17 January 2009 - 02:51 AM

hi ... how can i read multiple records from database ... for example ..
Chocolates is a type 1 in type table .. and in candies table there are 3 records of this type mean 3 names ... soo now if user search the candie by type so answer should be those 3 thngs in the same place.. like textbox, or listbox...

so far.. what i understand is below.. evry time it gives the item name but only last one ...not the first 2... hope u understand ..thanx

 string str = "select CName from Candies where TID= " + textBox1.Text;

			OleDbCommand command = new OleDbCommand(str, conn);
			OleDbDataReader reader = command.ExecuteReader();

			while (reader.Read())
			{

			   textBox3.Text = reader["CName"].ToString();

			  // textBox3.Text=Convert.ToString(reader.Getvalue(0));
			   //textBox3.Text=Convert.ToString(reader.Getvalue(1));


			}



Is This A Good Question/Topic? 0
  • +

Replies To: Reading Multiple records from database

#2 Kuggi   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 80
  • Joined: 16-January 09

Re: Reading Multiple records from database

Posted 17 January 2009 - 03:03 AM

View Postafridi2129, on 17 Jan, 2009 - 01:51 AM, said:

hi ... how can i read multiple records from database ... for example ..
Chocolates is a type 1 in type table .. and in candies table there are 3 records of this type mean 3 names ... soo now if user search the candie by type so answer should be those 3 thngs in the same place.. like textbox, or listbox...

so far.. what i understand is below.. evry time it gives the item name but only last one ...not the first 2... hope u understand ..thanx

 string str = "select CName from Candies where TID= " + textBox1.Text;

			OleDbCommand command = new OleDbCommand(str, conn);
			OleDbDataReader reader = command.ExecuteReader();

			while (reader.Read())
			{

			   textBox3.Text = reader["CName"].ToString();

			  // textBox3.Text=Convert.ToString(reader.Getvalue(0));
			   //textBox3.Text=Convert.ToString(reader.Getvalue(1));


			}



You can make an ArrayList and collect the information.
Was This Post Helpful? 0
  • +
  • -

#3 afridi2129   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Re: Reading Multiple records from database

Posted 17 January 2009 - 03:10 AM

why array list.. i have to just read from data base type column... its window form thing..
Was This Post Helpful? 0
  • +
  • -

#4 Kuggi   User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 80
  • Joined: 16-January 09

Re: Reading Multiple records from database

Posted 17 January 2009 - 03:16 AM

View Postafridi2129, on 17 Jan, 2009 - 02:10 AM, said:

why array list.. i have to just read from data base type column... its window form thing..


I understanded your question as you wanted to return multi records from the database.. if so.. you can return an ArrayList and then run an Foreach to show the content to the user.
Was This Post Helpful? 0
  • +
  • -

#5 afridi2129   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Re: Reading Multiple records from database

Posted 17 January 2009 - 04:11 AM

hmm u mean to say first i have to read from datbase and put it in array and then shows data... WHy??
doing this way i have to first get n put in array list ... now there is a checking process that which type of things user want to be displayed... i thnk lengthy process...

there must be a way which in reader.Read area which will read multiple rows of one column...


searching...

This post has been edited by afridi2129: 17 January 2009 - 04:12 AM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Reading Multiple records from database

Posted 17 January 2009 - 08:11 AM

View Postafridi2129, on 17 Jan, 2009 - 03:51 AM, said:

time it gives the item name but only last one ...not the first 2

Not exactly. With the code you offered, it's reading each record and loading the field with it. You just see the last record because that's the last one it wrote.

You could do something like:
textBox3.Text = (reader.Read()) ? reader["CName"].ToString() : "";
textBox4.Text = (reader.Read()) ? reader["CName"].ToString() : "";
textBox5.Text = (reader.Read()) ? reader["CName"].ToString() : "";



Here what we're doing is rather than while, we're using if and reading the record, if we got one.

If you're reading multiple records, the magic word you may wish google is DataSet.
Was This Post Helpful? 0
  • +
  • -

#7 afridi2129   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 04-November 07

Re: Reading Multiple records from database

Posted 17 January 2009 - 10:58 PM

thanx alot Sir... i got this one... and successfull....


I thnk I m in Love with this Community :P....

This post has been edited by afridi2129: 17 January 2009 - 10:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1