calling class method

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) %>">
 
G

Guest

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
 
G

Guest

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);
 
S

Scott Allen

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.
 
G

Guest

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) %>">
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
IRMNikole

Latest Threads

Top