p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/asp-net-3-5-basics-351/)
-   -   How to bind data from a db to a label in c# 3.5 (http://p2p.wrox.com/asp-net-3-5-basics/73016-how-bind-data-db-label-c-3-5-a.html)

bex March 2nd, 2009 08: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.

thanks

alliancejhall March 2nd, 2009 02: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 02:34 PM

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()


alliancejhall March 2nd, 2009 02:47 PM

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();


bex March 2nd, 2009 04: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 04:37 PM

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.

bex March 3rd, 2009 03: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

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

gbianchi March 3rd, 2009 05: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 12: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
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

gbianchi March 3rd, 2009 12:25 PM

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.


All times are GMT -4. The time now is 05:42 AM.

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