views:

16

answers:

1

hi I've developed an application using strong-typed dataset with .net framework 3.5. is there a way to change the source table for a tableadapter programmatically? thnx

A: 

There are a couple of ways that you can do this. First you could just add a new query that pulls from the different table, and then execute the method for that query, as long as the columns match it will work.

If you need to dynamically change the one of the statements you can access the command collection of the table adapter, it is protected though, so the easiest way to do this is to create a partial class to extend the one generated by the designer. Once you do this you can add your own method to return the data. You can use adapter.CommandCollection[0].CommandText to get and set the SQL for the the default GetData command that is created.

Once you do this you can change it, clear out the parameters, add new parameters or whatever you want to do, then you set the CommandText with the altered SQL, and call GetData or whatever you named the command and it will execute and return as usual.

Here is a code example:

using System.Data.SqlClient;
namespace DataTableAdapters
{
    public partial class Data_ItemTableAdapter
    {

        public Data.Data_ItemDataTable GetDynamicExample(string SearchValue)
        {

            using (Data_ItemTableAdapter a = new Data_ItemTableAdapter())
            {
                SqlCommand cmd = a.CommandCollection[0];
                cmd.Parameters.Clear();
                string SQL = @"Select Data_Item_ID, Data from Data_Item where
                        SearchValue = @SearchValue";
                cmd.CommandText = SQL;
                cmd.Parameters.AddWithValue("@SearchValue", SearchValue);
                return a.GetData();

            }

        }
    }
}
Chris Mullins