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)

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.

All times are GMT -4. The time now is 09:15 AM.

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