Return a DataSet for Web Application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R3JlZw==?=

    Return a DataSet for Web Application

    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.Data Source = dsMyDataSource;
    myGridView.Data Bind();

    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 = ConfigurationMa nager.Connectio nStrings
    "DBName"].ConnectionStri ng;
    SqlConnection myConn = new SqlConnection(s trConn);
    string strSelectSQL = "SELECT * FROM tUser";
    SqlCommand cmdUser = new SqlCommand(strS electSQL, myConn);

    SqlDataAdapter adpUser = new SqlDataAdapter( );
    adpUser.SelectC ommand = cmdUser;

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

    Now, I get an error "An object reference is required for the non-static
    field, method, or property "UserComponents .UserDB.m_strCo nn"" 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
    ObjectDataSourc e 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.Dat aSource using the procedure I
    have above.

    Thanks.
  • Alberto Poblacion

    #2
    Re: Return a DataSet for Web Application

    "Greg" <AccessVBAnet@n ewsgroups.nospa mwrote in message
    news:D9B765B5-EAAA-4A62-A912-4068D5C6CC6E@mi crosoft.com...
    [...]
    public static DataSet GetDataSet()
    {
    string strConn = ConfigurationMa nager.Connectio nStrings
    "DBName"].ConnectionStri ng;
    SqlConnection myConn = new SqlConnection(s trConn);
    string strSelectSQL = "SELECT * FROM tUser";
    SqlCommand cmdUser = new SqlCommand(strS electSQL, myConn);
    >
    SqlDataAdapter adpUser = new SqlDataAdapter( );
    adpUser.SelectC ommand = cmdUser;
    >
    DataSet dsUser = new DataSet();
    adpUser.Fill(ds User);
    return dsUser;
    }
    >
    Now, I get an error "An object reference is required for the non-static
    field, method, or property "UserComponents .UserDB.m_strCo nn"" 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").

    >
    I'm trying this approach because populating a GridView control using the
    ObjectDataSourc e 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.Dat aSource using the procedure
    I
    have above.
    >
    Thanks.

    Comment

    Working...