p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   How to bind data from a db to a label in c# 3.5 (http://p2p.wrox.com/showthread.php?t=73016)

bex March 2nd, 2009 09:53 AM

How to bind data from a db to a label in c# 3.5
Hi there i need to bind data to a label like i did ask before in VB 2.0, i tryed to convert the code my self but it didnt work.


alliancejhall March 2nd, 2009 03:18 PM

What code?
What code are you using? could you post it here and maybe would could help you use what you already have?

bex March 2nd, 2009 03:34 PM

Hi this is the code:

protectedvoid Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
SqlDataReader MyReader;
SqlConnection MyConnection = newSqlConnection();
MyConnection.ConnectionString =
SqlCommand MyCommand = newSqlCommand();
MyCommand.CommandText = "SELECT HomeLeft FROM Home";
MyCommand.CommandType = CommandType.Text;
MyCommand.Connection = MyConnection;
MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
  //this.Label1.DataSource = MyReader;

if i use a listbox or gridview it works but i want to use a label,

this is what i have in vb 2.0

Dim con AsNew SqlConnection(ConfigurationManager.ConnectionStrings("test").ConnectionString)
Dim cmdd AsNew SqlCommand("select NumberEmailSent from AsigntUsers Where Name= @Name")
Dim dr As SqlDataReader = Nothing
cmdd.Parameters.Add("@Name", Data.SqlDbType.NVarChar, 50).Value = Me.lblUser.Text
cmdd.Connection = con
dr = cmdd.ExecuteReader()
If dr.HasRows() = TrueThen
Me.sent.Text = dr("NumberEmailSent").ToString

alliancejhall March 2nd, 2009 03:47 PM

How about this?
I'm not totally strong on C# but I "think" this is what you want


    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings("Minds").ConnectionString);
    SqlCommand cmdd = new SqlCommand("Select HomeLeft from Home");
    SqlDataReader dr = null;
    cmdd.Connection = con;
    dr = cmdd.ExecuteReader();
    if (dr.HasRows() == true) {
        this.label1.Text = dr("HomeLeft").ToString;

bex March 2nd, 2009 05:25 PM

thanks but didnt work.
it says (dr is a variable but is used as method)

and the dr.hasrows
(is a property but is used as method)

gbianchi March 2nd, 2009 05:37 PM

then change the code! it's not that hard...


if (dr.HasRows() == true)

if (dr.HasRows == true)
and you have to do getvalue(or getstring or getint) inside the loop


this.label1.text = dr.gettext(dr.getcolumnofset("HomeLeft")
the property is not getcolumnoffset, but is one that return the number of the column based on it's name.

bex March 3rd, 2009 04:59 AM

hi thanks for the translation ,
i have an error saying that i miss a using directive or assembly reference.
i have imported system.data.sqlclient, what else i need to import?
this is the page


using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
publicpartialclass_Default : System.Web.UI.Page
protectedvoid Page_Load(object sender, EventArgs e)
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionString("Minds").ConnectionString);
SqlCommand cmdd = newSqlCommand("Select HomeLeft from Home");
SqlDataReader dr = null;
cmdd.Connection = con;
dr = cmdd.ExecuteReader();
if (dr.HasRows == true)
this.Label1.Text = dr.gettext(dr.getcolumnofset("HomeLeft"));

i want to take the HomeLeft column that has Nvarchar(50)dt and display it in Label1, HomeLeft is Column number 2 in the table, so getcolumnofset should be replaced with what?

gbianchi March 3rd, 2009 06:34 AM

Hey, help us a little.. I told you that the property was not getcolumnoffset, can you just look for the correct function? you can use intellisense, msdn, google, just look for the correct one. In fact, gettext is wrong too (I was just giving you the general idea).

bex March 3rd, 2009 01:17 PM

hi i did try the intelesence and used the tostrin methode all it does is showing the connection string not the data,
in vb the column is called once
i dont know how it works in C# that wee have it twice
Label1.Text = dr.gettext(dr.getcolumnofset("HomeLeft"));

sory for the ignorance but i started C# 1 week ago

gbianchi March 3rd, 2009 01:25 PM

the correct line is

label1.text = dr.getstring(dr.getordinal("HomeLeft"));
This function fail is the value of the data is null, so check for null values before doing this.

You have it twice because getstring (or value, or XXX) require the number of the column. Because it's suicidal to supply the number by itself (maybe you know it, but hey, another can change the code) then you suply it with getordinal that will return the number for the column name you are suppling.

Imar March 3rd, 2009 01:40 PM

Don't forget that C# is case sensitive, so it should probably be:

label1.Text = dr.GetString(dr.GetOrdinal("HomeLeft"));


gbianchi March 3rd, 2009 01:44 PM

Well.. I'm case insensitive. [:D]

Maybe that's why intellisense didn't help the OP?

bex March 3rd, 2009 02:28 PM

Thanks , can i ask u something?
i have 6 lablels that i need to bind, i am a bit wored about performance if i have to open 6 connections and close them that is not something i want to do, is there a way to do this under one connection? or use deferent controls that can do this in a lighter weight?

My table has 6 columns and 1 row i want to bind each column into labels/asp:conntrols

can i do it like this:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionString("Minds").ConnectionString);
SqlCommand cmdd = newSqlCommand("Select HomeLeft, Right, Centre from Home");
SqlDataReader dr = null;
cmdd.Connection = con;
dr = cmdd.ExecuteReader();
if (dr.HasRows == true)

label1.Text = dr.GetString(dr.GetOrdinal("HomeLeft"));
label2.Text = dr.GetString(dr.GetOrdinal("Right"));
label3.Text = dr.GetString(dr.GetOrdinal("Centre"));.....


Imar March 3rd, 2009 03:14 PM

The obvious answer is: "Have you tried it out"?

It looks good to me, and it should work like this...


bex March 3rd, 2009 04:48 PM

yes it worked that was write

Imar March 3rd, 2009 04:51 PM

Well, you wrote: "can i do it like this:" implying you hadn't tried or tested it yet.

But then what is your question?


bex March 4th, 2009 05:17 AM

it sounds a bit silly , that was part of my question, what other controls can i use to achieve the same thing?
this time i am not gonna ask if i can use html labels before trying[:)].

gbianchi March 4th, 2009 06:41 AM

To achieve what? showing a text on screen?? anything that display a text :). Can you rephrase or think out load what are you expecting to accomplish?

alliancejhall March 4th, 2009 09:34 AM

It might be easier...
It might be easier to use a repeater control and a sqldatasource?

and just let them do all the work.

gbianchi March 4th, 2009 09:36 AM

well. OP only has 1 row with several columns. That's a nice idea if you have several rows.

That's why I ask him to think out loud, maybe we can understand what is he trying to do.

bex March 4th, 2009 02:47 PM

I need to display plain text

i have 6 labels in different positions that displaying the column that is binded too, like label1.text=column1.

that is why i have the 6 columns and 1 row cos the table is not gonna grow.
i don't know if is possible to do it the other way round, 1 column and 6 rows.

any way i have it up and running by using labels,

thank you guys

gbianchi March 4th, 2009 03:18 PM

If your table will grow, the code we provide to you have to change. And your better solution is to use a repeater bounded to a datasource. You can't bind the same label to several rows!

bex March 4th, 2009 03:34 PM

i have 6 labels in the page that are positiont 2 on the top off the page 2 in the footer 1 left 1 right, can the repeater display text in this places??

the table will not grow

gbianchi March 4th, 2009 03:37 PM

Sorry. Didn't see the NOT.. then it's ok this way.

All times are GMT -4. The time now is 07:27 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.