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.

All times are GMT -4. The time now is 12:54 AM.

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