Solved

Handling Textbox Event

Posted on 2007-11-28
4
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

752 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