Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 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
 
Old April 12th, 2009, 09:28 PM
Authorized User
 
Join Date: Jan 2009
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
Default SqlDataAdapter issue

Hi people. I am currently facing a problem over here. Coming to know that SqlDatareader does not support paging, but SqlDataAdapter supports, i tried to convert the codes and i am quite stucked.

I want to do a count for the records in my database, but i did it using SqlDatareader, and i am not sure how to implement it with SqlDataAdapter.

which is this part
<hr />
string what = null;
while (reader.Read())
{
what = reader["TotalMessages"].ToString();
lblNoOfMsg.Text = "(" + what + ")";
}
reader.Close();
con.Close();
}
<hr />
USING SqlDataAdapter

privatevoid CountNoOfMessages()
{
string UserEmail = Session["UserEmail"].ToString();
string strConString = ConfigurationManager.ConnectionStrings["SocialSystemConnectionString"].ConnectionString;
SqlConnection myConnect = newSqlConnection(strConString);
//Create Adapter
string strCommandText = "SELECT COUNT(MessageID) AS TotalMessages FROM [Message] WHERE [Message].RecipientEmail= @Email AND TypeID = 'Inbox'";
SqlDataAdapter myAdapter = newSqlDataAdapter(strCommandText, myConnect);
//Create Dataset to store results of query
DataSet myDS = newDataSet();
myAdapter.Fill(myDS, "Message");
GridView2.DataSource = myDS.Tables["Message"];
GridView2.DataBind();

}

USING SqlDataReader

privatevoid CountNoOfMessages(){
SqlConnection con = newSqlConnection(strConString);
SqlCommand cmd = newSqlCommand("SELECT COUNT(MessageID) AS TotalMessages FROM [Message] WHERE [Message].RecipientEmail= @Email AND TypeID = 'Inbox'", con);
cmd.Parameters.Add("@Email", SqlDbType.VarChar);
cmd.Parameters["@Email"].Value = UserEmail;
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
string what = null;
while (reader.Read())
{
what = reader["TotalMessages"].ToString();
lblNoOfMsg.Text = "(" + what + ")";
}
reader.Close();
con.Close();
}

Last edited by julius; April 12th, 2009 at 09:32 PM..
 
Old April 12th, 2009, 11:15 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You don't need to use an adapter OR a reader for this.

Since you only need a count, which is a single number, you can just retrieve a scalar value.

Code:
private void CountNoOfMessages()
{
  SqlConnection con = newSqlConnection(strConString);
  SqlCommand cmd = newSqlCommand("SELECT COUNT(MessageID) AS   TotalMessages FROM [Message] WHERE [Message].RecipientEmail= @Email AND TypeID = 'Inbox'", con);
  cmd.Parameters.Add("@Email", SqlDbType.VarChar);
  cmd.Parameters["@Email"].Value = UserEmail;
  con.Open();
   lblNoOfMsg.Text = cmd.ExecuteScalar.ToString();
  con.Close();
}
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old April 13th, 2009, 12:31 AM
Authorized User
 
Join Date: Jan 2009
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
Default

Infact, another error occurs....

private
void CountNoOfMessages()
{
string UserEmail = Session["UserEmail"].ToString();
string strConString = ConfigurationManager.ConnectionStrings["SocialSystemConnectionString"].ConnectionString;
SqlConnection con = newSqlConnection(strConString);
SqlCommand cmd = newSqlCommand("SELECT COUNT(MessageID) AS TotalMessages FROM [Message] WHERE [Message].RecipientEmail= @Email AND TypeID = 'Inbox'", con);
cmd.Parameters.Add(
"@Email", SqlDbType.VarChar);
cmd.Parameters[
"@Email"].Value = UserEmail;
con.Open();
lblNoOfMsg.Text = cmd.ExecuteScalar.ToString();
con.Close();
}


CS0119: 'System.Data.Common.DbCommand.ExecuteScalar()' is a 'method', which is not valid in the given context
 
Old April 13th, 2009, 12:38 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Whoops, sorry!

lblNoOfMsg.Text = cmd.ExecuteScalar().ToString();
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
julius (April 13th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
sqldataadapter vgsgowrisankar C# 2005 7 April 23rd, 2008 04:32 PM
SqlDataAdapter DanRoche .NET Framework 1.x 3 August 20th, 2007 04:04 AM
SqlDataAdapter does not have its properties thaopham215 ASP.NET 2.0 Basics 13 December 6th, 2006 05:15 PM
SqlDataAdapter Issue Tom Wing VB.NET 2002/2003 Basics 2 February 6th, 2006 01:43 PM
sqlDataAdapter [email protected] ADO.NET 1 April 8th, 2004 12:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.