Return a DataSet for Web Application

G

Greg

Most of my background is with VB.Net and WinForm development and I am in the
process of migrating my skills to C# Web Based development. I've come across
something I'm not quite sure how I should handle.

I have a web-page with a asp:GridView on it. In hte Page_Load event of the
page, I create a Dataset and Bind it to the grid as follows:

myGridView.DataSource = dsMyDataSource;
myGridView.DataBind();

Now, this works OK in testing, but I need to move the DataSource to come
from a module.

I've created a module that builds a DataSet call GetDataSet(); I've created
as follows:

public static DataSet GetDataSet()
{
string strConn = ConfigurationManager.ConnectionStrings
"DBName"].ConnectionString;
SqlConnection myConn = new SqlConnection(strConn);
string strSelectSQL = "SELECT * FROM tUser";
SqlCommand cmdUser = new SqlCommand(strSelectSQL, myConn);

SqlDataAdapter adpUser = new SqlDataAdapter();
adpUser.SelectCommand = cmdUser;

DataSet dsUser = new DataSet();
adpUser.Fill(dsUser);
return dsUser;
}

Now, I get an error "An object reference is required for the non-static
field, method, or property "UserComponents.UserDB.m_strConn"" m_strConn is
already a valid connection string as I am using it in other procedures in the
same module. What does this error mean?

I'm trying this approach because populating a GridView control using the
ObjectDataSource control does not allow me to provide sorting, so I'm looking
to populate the DataSource for the grid using a Dataset. I'm just not sure
how I can pass a DataSet to the gridControl.DataSource using the procedure I
have above.

Thanks.
 
A

Alberto Poblacion

Greg said:
[...]
public static DataSet GetDataSet()
{
string strConn = ConfigurationManager.ConnectionStrings
"DBName"].ConnectionString;
SqlConnection myConn = new SqlConnection(strConn);
string strSelectSQL = "SELECT * FROM tUser";
SqlCommand cmdUser = new SqlCommand(strSelectSQL, myConn);

SqlDataAdapter adpUser = new SqlDataAdapter();
adpUser.SelectCommand = cmdUser;

DataSet dsUser = new DataSet();
adpUser.Fill(dsUser);
return dsUser;
}

Now, I get an error "An object reference is required for the non-static
field, method, or property "UserComponents.UserDB.m_strConn"" m_strConn is
already a valid connection string as I am using it in other procedures in
the
same module. What does this error mean?

The message means that you are calling the non-static variable m_strConn
from a static method, but it doesn't match the fragment of code that you
provided since there is no m_strConn there. The error has to be somewhere
else in your code.

Since you come from VB and you mention that you have written "a module",
it is worth mentioning that we don't have modules in C#; the closest
equivalent would be a static class. But if you mix both static and
non-static (instance) members in the same class, you need to be aware that
it doesn't make sense to call an instance member from a static one (which
specific instance would the static member be calling?). Don't think about a
module, think about a public class in VB, and do in C# the same things that
you would do in the VB class (replacing "Shared" with "static").
 

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

Top