Create an AccessDataSource in code (.cs)? RRS feed

  • Question

  • User421854095 posted

    Hello, I'm currently creating AccessDataSource using <asp:AccessDataSource> tag, however, I want to create an AccessDataSource in code, in order to be able to switch between DataSources when you click on button, could you help me out? I don't know how to create it on code, this is what I have so far:

    GridView1.DataSource = null;
    AccessDataSource ds1 = new AccessDataSource(); ds1.ConnectionString = ConfigurationManager.ConnectionStrings["myConnString"].ConnectionString; ds1.SelectCommand = "SELECT * FROM employees "WHERE first_name LIKE '%' + @test_param + '%' "; ds1.SelectParameters.Add("test_param", "GEORGE"); GridView1.DataSource = ds1; GridView1.DataBind();

    When I click on button, it tells me that "AccessDataSource ConnectionString property cannot be stablished, it is automatically generated", and I don't understand what's happening, I'm using ConnectionString for everything, but now is giving me problems, thanks in advanced.

    EDIT: I have a workaround, instead of creating new AccessDataSources, I will change the SelectCommand, which is the only one I need, does that work?

    String testSql = "SELECT * FROM jobs WHERE job_type = @test_param";
    GridView1.DataSource = null;
    GridView1.DataSourceID = "AccessDataSource7";
    //AccessDataSource already created
    AccessDataSource7.SelectCommand = testSql;
    AccessDataSource7.SelectParameters.Add("test_param", "ENGINEERING");

    Would this work? so far is working :D

    Thursday, May 10, 2012 12:53 PM