Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to display database data from Access into a label. Here is my code:

C#
string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=D:/_Web3rev/App_Data/CentralDB.mdb";
OleDbConnection cn = new OleDbConnection(connectString);

cn.Open();
string selectString = "SELECT studid FROM Students WHERE studid='" + lblStudid.Text+ "'";
OleDbCommand cmd = new OleDbCommand(selectString, cn);
OleDbDataReader reader = cmd.ExecuteReader();




I want to display database content into label as per user entry. Thanks.
Posted
Comments
[no name] 7-Feb-13 4:08am    
So where so you stuck ?
Ed Gepulle 7-Feb-13 7:08am    
I want to display the studid of that particular user to the lblstudid.txt. what is the good code in displaying such data? the above code already opened but not yet displaying the data with that studid.
Ed Gepulle 7-Feb-13 7:36am    
here is the code i am trying to implement but it went wrong, still the data from database isnt displaying:

string connectString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=D:/_Web3rev/App_Data/CentralDB.mdb";
OleDbConnection cn = new OleDbConnection(connectString);

cn.Open();
string selectString = "SELECT studid FROM Students WHERE studid='" + lblStudid.Text+ "'";
OleDbCommand cmd = new OleDbCommand(selectString, cn);
OleDbDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
//txtStudid.Text = reader["studid"].ToString();
cmd.Parameters.AddWithValue("@studid", txtStudid.Text);
cmd.Parameters.AddWithValue("@fullname", txtName.Text);
}
reader.Close();
cn.Close();
CHill60 7-Feb-13 8:57am    
You're adding parameters to the sql cmd after you've run ExecuteReader but not actually getting the results from reader e.g. lblstudid.text = reader.GetString(reader.GetOrdinal("studid"));
But why bother getting studid from the database when you already have it in lblStudid.Text (see selectString)
Ed Gepulle 7-Feb-13 9:48am    
I wanted to display the corresponding data of the studid from the db. Display it on a label or textbox will be good.

1 solution

As per the comments above, this worked
SQL
if(reader.Read())
     lblstudid.text = reader.GetString(reader.GetOrdinal("studid"));
and get rid of the while {}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900