Solved

Handling Textbox Event

Posted on 2007-11-28
4
370 Views
Last Modified: 2007-12-04
This is not an exam or Homework

You have written the following code segment for a Web form:

Dim con As New SqlConnection(conString)
con.Open()
Dim cmd As New SqlCommand("SELECT Count(*) As AuthorsCount FROM Authors", con)

You are supposed to display the number of authors in a Label control (Label1). Which of the following code snippets will achieve the stated goal?

1.
Label1.Text = cmd.ExecuteScalar()
con.Close()
 
2.
Label1.Text = cmd.ExecuteSQL()
con.Close()
 
3.
Label1.Text = cmd.ExecuteNonQuery()
con.Close()
 
4.
Label1.Text = cmd.ExecuteReader().Item("AuthorsCount")
con.Close()
0
Comment
Question by:cybernation989
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
Roopesh_7 earned 500 total points
ID: 20368087
Label1.Text = cmd.ExecuteScalar()
con.Close()
0
 

Author Comment

by:cybernation989
ID: 20380244
Thanks Roopesh!

But after reading a number of sites I think number 1 would just show the first row...

ExecuteScalar - Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

so are you sure it will display the number of authors in a Label control (Label1)?
0
 

Author Comment

by:cybernation989
ID: 20380292
Guess I was wrong...

it shows how you can count the rows with executescalar at
http://forums.asp.net/p/1142892/1847306.aspx

so I guess you were right... still a little confused on how it works
0
 
LVL 6

Expert Comment

by:Roopesh_7
ID: 20382416


Hey friend,
Try this with an exaple so you will find:)

Thanks
Roops
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question