Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 23rd, 2006, 09:02 AM
wk wk is offline
Authorized User
 
Join Date: Aug 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default System.Data.SqlClient.SqlDataReader - URGENT

Dear all,

 I'm using sqldatareader to return data that i need and then bind it to the gridview. Anyway, I'm facing the error message " 'System.Data.SqlClient.SqlDataReader' has no constructors defined".

Please help, thanks.



Levine


Reply With Quote
  #2 (permalink)  
Old September 23rd, 2006, 05:30 PM
Authorized User
 
Join Date: Jan 2006
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My understanding of the DataReader object is that you don't create a new instance of it, like this:
Code:
SqlDataReader myReader = new SqlDataReader();
This will give you the error, "'System.Data.SqlClient.SqlDataReader' has no constructors defined".

Instead, you assign an instance of the DataReader object to a SqlCommand command:
Code:
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;

SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = strConnectionString;

string strCommandText = "SELECT FirstName, LastName FROM Users;

SqlCommand myCommand = new SqlCommand(strCommandText, myConnection);

myConnection.Open();
SqlDataReader myReader = myCommand.ExecuteReader(); // <-- *** Here is the instantiation
while (myReader.Read())
{
    // process the rows in myReader using
    // Convert.ToString(myReader["FirstName"]) and
    // Convert.ToString(myReader["LastName"])
}
myReader.Close();
Reply With Quote
  #3 (permalink)  
Old September 26th, 2006, 01:50 AM
wk wk is offline
Authorized User
 
Join Date: Aug 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi RobC,

Thanks for that. But may I know how can I return the result set and bind it to gridview? Below is my code.

public method in Employee class file

public SqlDataReader A(String EmpID)
{
 SqlConnection con = new SqlConnection("myconnection");
 con.Open();
 SqlCommand cmd = new SqlCommand("", con);

 cmd.CommandText = "SELECT Emp_Name, Emp_Add from " + EmpID + " WHERE UPPER(Emp_Name) IN (SELECT UPPER(Emp_Name) as Name FROM " + EmpID + " GROUP BY Emp_Name HAVING (COUNT(UPPER(Emp_Name)) > 1)) ORDER BY Emp_Name";

SqlDataReader dtr = cmd.ExecuteReader();
return dtr;
 }

The code that I have in asp.cs is as below:

Employee myEmp = new Employee();
grv.DataSource = A ;
grv.DataBind();

May I know normally is the sqldatareader able to read a sqlcommand that have input parameter and return the result set and bind it to gridview? or is there a better way to handle this kind of situation instead of using sqldatareader?

thanks in advance


Levine


Reply With Quote
  #4 (permalink)  
Old September 26th, 2006, 09:53 AM
Authorized User
 
Join Date: Jan 2006
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This line doesn't look right:
Code:
grv.DataSource = A ;
I think it should be something like:
Code:
grv.DataSource = myEmp.A(<someString>);
Reply With Quote
  #5 (permalink)  
Old September 26th, 2006, 11:04 AM
wk wk is offline
Authorized User
 
Join Date: Aug 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again RobC :)

I managed to convert the data get from sqldatareader to dataset and bind it to gridview , but I'm facing problem in getting the 'paging' in my gridview. Does anyone have any ideas? Thanks in advance.

Levine

Reply With Quote
  #6 (permalink)  
Old September 26th, 2006, 11:10 AM
Authorized User
 
Join Date: Jan 2006
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just set the AllowPaging property of the DataGrid to true. It's in the Properties panel.
Reply With Quote
  #7 (permalink)  
Old September 26th, 2006, 11:41 AM
wk wk is offline
Authorized User
 
Join Date: Aug 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi there,

Below is the error message I got when I tried to click on page 2 :
The GridView 'GridView1' fired event PageIndexChanging which wasn't handled. Any ideas?

Levine



Reply With Quote
  #8 (permalink)  
Old September 26th, 2006, 02:29 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

it sounds like you do not have a method that handles your PageIndexChanging event.

--Stole this from a moderator

I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.
Reply With Quote
  #9 (permalink)  
Old September 27th, 2006, 06:00 AM
wk wk is offline
Authorized User
 
Join Date: Aug 2006
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Problem solved :)

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing System.Data.SQLClient dll mike_abc VB.NET 2 June 3rd, 2011 11:18 PM
'SYSTEM.DATA.SQLCLIENT.SQLEXCEPTION' soorsha General .NET 0 September 24th, 2006 12:59 AM
System.Data.SqlClient.SqlException: Login failed f belete SQL Server 2005 3 May 18th, 2006 09:18 AM
System.Data.SqlClient.SqlException kbarsi Beginning VB 6 2 August 13th, 2005 10:39 PM
Urgent getting Error while using SqlDataReader hums ADO.NET 2 September 29th, 2004 11:03 AM



All times are GMT -4. The time now is 07:50 AM.


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