calling class method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a mothod in a class that return a datareader object as show below and
trying to assign the datareader object to a datagrid but getting this error
message:

CS0120: An object reference is required for the nonstatic field, method, or
property 'intranet.discussion.discussion.GetThreads(int)'


public SqlDataReader GetThreads(int TopicID)
{
SqlConnection objConn = new
SqlConnection(ConfigurationSettings.AppSettings["DNS"]);
objConn.Open();
SqlCommand objCmd = new SqlCommand("sp_getNoticeReply",objConn);
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Parameters.Add("@TopicID",TopicID);
SqlDataReader objReader = objCmd.ExecuteReader();
return objReader;
}

<%@ Import Namespace="intranet.discussion" %>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 312px; POSITION:
absolute; TOP: 272px"
runat="server" DataSource="<%# discussion.GetThreads(4) %>">
 
Hi,
You need to first create an instance of the discussion class and then call
the <instancename>.GetThreads since the GetThreads method is not static.
HTH
srini
 
you cann't assign a datareader to a datagrid. or at list this is what i know
till today.
you shoud assign a dataset, datatable or an arraylist as a datasource . but
not a sqldatareader.
plus, if you don't close the reader you will get another error because you
didn't close the reader. The simplest way to solve your problem is to return
a datatable to use data adapter and fill method for adding data to data
table. and do not forget to close all readers, adapters and connection before
return.

SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(query, conn);
adapter.Fill(datatable);
 
You can bind a web form DataGrid to a SqlDataReader, but it does
sacrifice some features (like built in paging).

The original poster's problem was because there was no instance of the
discussion class to invoke GetThreads on.
 
Hi srini
can you please tell me how create an instance of the discussion class? i
know how to create in code-behind but not sure how you do it in .aspx page

srini said:
Hi,
You need to first create an instance of the discussion class and then call
the <instancename>.GetThreads since the GetThreads method is not static.
HTH
srini

huzz said:
I have a mothod in a class that return a datareader object as show below and
trying to assign the datareader object to a datagrid but getting this error
message:

CS0120: An object reference is required for the nonstatic field, method, or
property 'intranet.discussion.discussion.GetThreads(int)'


public SqlDataReader GetThreads(int TopicID)
{
SqlConnection objConn = new
SqlConnection(ConfigurationSettings.AppSettings["DNS"]);
objConn.Open();
SqlCommand objCmd = new SqlCommand("sp_getNoticeReply",objConn);
objCmd.CommandType = CommandType.StoredProcedure;
objCmd.Parameters.Add("@TopicID",TopicID);
SqlDataReader objReader = objCmd.ExecuteReader();
return objReader;
}

<%@ Import Namespace="intranet.discussion" %>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 312px; POSITION:
absolute; TOP: 272px"
runat="server" DataSource="<%# discussion.GetThreads(4) %>">
 
Back
Top