 |
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 2nd, 2009, 09:53 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2008
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
|
|
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:
|
|
|

March 2nd, 2009, 03:18 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
|
|
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
|
|

March 2nd, 2009, 03:34 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2008
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
|
|
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
|
|

March 2nd, 2009, 03:47 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 207
Thanks: 2
Thanked 15 Times in 15 Posts
|
|
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
|
|

March 2nd, 2009, 05:25 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2008
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
|
|
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
|
|

March 2nd, 2009, 05:37 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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.
================================================== =========
|
|

March 3rd, 2009, 04:59 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2008
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
|
|
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
|
|

March 3rd, 2009, 06:34 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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.
================================================== =========
|
|

March 3rd, 2009, 01:17 PM
|
|
Friend of Wrox
|
|
Join Date: Aug 2008
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
|
|
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
|
|

March 3rd, 2009, 01:25 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
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.
================================================== =========
|
|
 |