I assume that you use code-behind technique to split html and C#-code.
I have never used SqlDataReader as DataSource in UI layer. I write layered
code and hide the data source from UI layer. I use strongly typed
collections that I fill in DAL.
Few things to remember.
1) You need to call DataBind() on all objects or thsi.DataBind(); to bind
your data source with controls
2) You need to close connection object, but don't close it too early i.e.
when you need the dataReader-object.
public class YourPage : Page
{
protected SqlDataReader dr;
public void BindData()
{
objConn.Open();
SqlCommand selectCmd = new SqlCommand("SELECT * FROM tblFault WHERE
ID=" + Request.QueryString["id"] ,objConn);
dr = selectCmd.ExecuteReader();
this.DataBind();
// I am not exactly sure can we close the dr here. Is the prev.
expression done its job?
dr.Close();
objConn.Close();
}
JMu
public class
"huzz" <hu**@discussions.microsoft.com> wrote in message
news:B7**********************************@microsof t.com...
sorry Jarmo.. i am new to asp.net can you show me with code?
"Jarmo Muukka" wrote:
Your dr exist in inside of method BindData(). You're trying to use it out
of
scope. To make it work, move it as protected member of your page class.
JMu
"huzz" <hu**@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com... >I am having problem accessing a datareader from the aspx page here is my
>code:
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> BindData();
> }
>
> public void BindData()
> {
>
> objConn.Open();
> SqlCommand selectCmd = new SqlCommand("SELECT * FROM
> tblFault
> WHERE ID=" + Request.QueryString["id"] ,objConn);
> SqlDataReader dr = selectCmd.ExecuteReader();
> dr.Read();
> // dr.Close();
> objConn.Close();
> }
>
> when i try to call the dr in the aspx page i get error message:
> The name 'dr' does not exist in the class or namespace
> 'ASP.details_aspx'
> ..
> but it does exist
>
> Here is the code i am using to call the dr in the aspx page
> (I am imprting system.data and system.data.sqlclient namespace aswell)
> <td><%# dr["ID"]%></td>
>
> any idea why i am getting this error message?
>