Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 2nd, 2009, 08:53 AM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default 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.

thanks
__________________
bx
The Following User Says Thank You to bex For This Useful Post:
Forum1dotnet (March 5th, 2009)
  #2 (permalink)  
Old March 2nd, 2009, 02:18 PM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default What code?

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

Follow me on Twitter @jhall2013
  #3 (permalink)  
Old March 2nd, 2009, 02:34 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

Hi this is the code:
Code:
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlDataReader MyReader;
SqlConnection MyConnection = newSqlConnection();
MyConnection.ConnectionString =
ConfigurationManager.ConnectionStrings["Minds"].ConnectionString;
SqlCommand MyCommand = newSqlCommand();
MyCommand.CommandText = "SELECT HomeLeft FROM Home";
MyCommand.CommandType = CommandType.Text;
MyCommand.Connection = MyConnection;
MyCommand.Connection.Open();
MyReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);
  //this.Label1.DataSource = MyReader;
  //thisLabel1.DataBind();
MyCommand.Dispose();
MyConnection.Dispose();
 
}
}
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
Code:
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
con.Open()
dr = cmdd.ExecuteReader()
dr.Read()
If dr.HasRows() = TrueThen
Me.sent.Text = dr("NumberEmailSent").ToString
EndIf
con.Close()
__________________
bx
  #4 (permalink)  
Old March 2nd, 2009, 02:47 PM
Friend of Wrox
 
Join Date: Nov 2007
Location: Central Florida, USA.
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
Default How about this?

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

Code:
    
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings("Minds").ConnectionString);
    SqlCommand cmdd = new SqlCommand("Select HomeLeft from Home");
    SqlDataReader dr = null;
    cmdd.Connection = con;
    con.Open();
    dr = cmdd.ExecuteReader();
    dr.Read();
    if (dr.HasRows() == true) {
        this.label1.Text = dr("HomeLeft").ToString;
    }
    con.Close();
__________________
Jason Hall

Follow me on Twitter @jhall2013
  #5 (permalink)  
Old March 2nd, 2009, 04:25 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

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)
__________________
bx
  #6 (permalink)  
Old March 2nd, 2009, 04:37 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,190
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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

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

Code:
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.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #7 (permalink)  
Old March 3rd, 2009, 03:59 AM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

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

Code:
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;
con.Open();
dr = cmdd.ExecuteReader();
dr.Read();
if (dr.HasRows == true)
{
this.Label1.Text = dr.gettext(dr.getcolumnofset("HomeLeft"));
}
con.Close();
}
 
}
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?
thanks
__________________
bx
  #8 (permalink)  
Old March 3rd, 2009, 05:34 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,190
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

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).
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #9 (permalink)  
Old March 3rd, 2009, 12:17 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Location: london, , United Kingdom.
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default

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
Label1.text=dr("NumberEmailSent").ToString
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
__________________
bx
  #10 (permalink)  
Old March 3rd, 2009, 12:25 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,190
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

the correct line is
Code:
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.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to update a label controll from a db bex ASP.NET 2.0 Professional 20 February 3rd, 2009 07:28 AM
Bind a Label cp75 ASP.NET 1.0 and 1.1 Basics 19 January 10th, 2007 09:53 AM
bind a image filename stored in DB wth FORMSVIEW alexdcosta ASP.NET 2.0 Basics 0 June 22nd, 2006 05:30 AM
Bind Data to a Single Label Control: Possible? jumpseatnews ASP.NET 2.0 Basics 1 April 6th, 2006 11:12 PM
how to bind a data() lsxx Classic ASP Components 4 April 20th, 2005 02:35 AM



All times are GMT -4. The time now is 09:19 PM.


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