Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Displaying a datasource field with a label

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James R. Davis - 01 Dec 2007 20:03 GMT
I am an absolute beginner and finding myself confused at every step that I
take in the learning process.  It will get better.

Anyway, I have a SqlDataSource control on a form and at page load time I
want to display the contents of a field from the record set using a label
control.

I cannot for th elife of me figure out how to reference that field.

The datasource select command simply reads the count(*) of records in the
table via:

SELECT Count(*) As RcdCnt

What I want to do is simply assign that value to Label1.Text but lack of
familiarity with ASP.NET has stopped me cold.  I expected to be able to say
something like:

Label1.Text = SqlDataSource1("RcdCnt")

Nope.

I know that there are all levels of knowledge and experience on this news
feed and sincerly hope that a rank beginner's question like this is welcome.
laziers@gmail.com - 01 Dec 2007 20:55 GMT
> What I want to do is simply assign that value to Label1.Text but lack of
> familiarity with ASP.NET has stopped me cold.  I expected to be able to say
> something like:
>
> Label1.Text = SqlDataSource1("RcdCnt")

Hi,

try this way

           using (SqlConnection conn = new
SqlConnection(connectionString))
           {
               conn.Open();

               string query = "select COUNT(*) from TABLE_NAME";

               SqlDataAdapter da = new SqlDataAdapter(query, conn);

               DataTable dt = new DataTable();

               da.Fill(dt);

               Label1.Text = dt.Rows[0].ToString();
           }

I hope that will helps You.

BTW, check this site: http://asp.net/learn/
For me, it is great site for the peoples who starts learning asp.net.
Milosz Skalecki [MCAD] - 01 Dec 2007 23:57 GMT
Howdy,

Many ways of doing the task at hand, one possible:

string connectionString = "server=serv;database=db;uid=userId;pwd=passw";
int count = 0;

using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(
        "SELECT Count(*) As RcdCnt FROM whatever", connection);
    count = (int) command.ExecuteScalar();
}

label1.Text = count.ToString();

Hope it helps
Signature

Milosz

> I am an absolute beginner and finding myself confused at every step that I
> take in the learning process.  It will get better.
[quoted text clipped - 20 lines]
> I know that there are all levels of knowledge and experience on this news
> feed and sincerly hope that a rank beginner's question like this is welcome.
James R. Davis - 02 Dec 2007 01:01 GMT
Once again, Miloz, thank you!

I used the ExecuteScalar method without any trouble at all.  In fact, that
was the last piece of the puzzel for my current page.  Now off to the next
one!

It's the bits and pieces of practical, everyday, useful information that
aggregates into a 'tool bag' full of capabilities and you have added some
fine ones to mine.

Jim

> Howdy,
>
[quoted text clipped - 13 lines]
>
> Hope it helps

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.