Click here to Skip to main content
12,073,325 members (74,054 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# database
I want to display database data from Access into a label. Here is my code:

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 6-Feb-13 23:05pm
Comments
Krunal Rohit 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.
CHill60 7-Feb-13 10:45am
   
if(reader.Read())
lblstudid.text = reader.GetString(reader.GetOrdinal("studid"));
and get rid of the while {}
Ed Gepulle 8-Feb-13 1:06am
   
wow i liked that. let me try that coding.
Ed Gepulle 8-Feb-13 1:14am
   
Thanks a lot chill60. Its working and its implemented in my code.
CHill60 8-Feb-13 4:02am
   
I'll post it as the solution for any others that come this way

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 8 Feb 2013
Copyright © CodeProject, 1999-2016
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