Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I am using MS Access Database.
 
Design in MSAccess as follows;
 
Faculty_Name Text
 

My Code as follows in Load button;
 
when i click the Load button faculty_Name to be retrieved from the DB and displayed in the List box.
 
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source= E:\\Projects\\Faculty Schedule\\Faculty Schedule\\Data\\HIMTTESTing.mdb;";
 
OleDbConnection OleDbCon = new OleDbConnection(connstring);
OleDbCon.Open();
string sql1 = "select Faculty_Name from  Tb_SCH_Faculty_Details ";
OleDbCommand cmd = new OleDbCommand(sql1, OleDbCon);
OleDbDataReader reader = cmd.ExecuteReader();
{
   Lb_Faculty_Name.Items.Add(reader);
}
OleDbCon.Close(); 
when i run in the List box the Faculty_Name is not displaying.
 
from my above code what is the mistake.how can i do.please help me.
 
[Edit]Code block added by Jibesh[/Edit]
Posted 23-Jan-13 22:26pm
Edited 23-Jan-13 22:51pm
Jibesh16.7K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you have to add one by one records
while (reader.Read())
{
Lb_Faculty_Name.Items.Add(reader[0].tostring());
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you have to use loop statement to read the reader and add data to listbox i.e
 
while (reader.Read())
{
Lb_Faculty_Name.Items.Add(reader.GetString(0));
}
 

where reader.GetString()
get the row with all colums as given 'reader.GetString(0)' gets the first colum value of the current row
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 DamithSL 270
1 OriginalGriff 184
2 BillWoodruff 170
3 Zoltán Zörgő 165
4 Sergey Alexandrovich Kryukov 125
0 OriginalGriff 7,760
1 DamithSL 5,874
2 Sergey Alexandrovich Kryukov 5,444
3 Maciej Los 5,076
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100