views:

378

answers:

2

I have a project that utilizes a number of FormViews and SQLDataSources. I would like the default behavior of the SQLDataSource to insert values that are empty as null. I know I can do this by creating an insert parameter for each parameter, but this gets really old for having to do this for update and insert on every parameter that could be empty.

<InsertParameters>
    <asp:Parameter Name="Name" ConvertEmptyStringToNull="true" />
</InsertParameters>

Is there any setting in the web.config or on the SQLDataSource I could set?

A: 

Override "Selecting" event and assign all what you need

Dewfy
A: 

I always derive a new class from Parameter or even SqlDataSource to change my desired default behaviour.

public class MyDataSource : SqlDataSource
{        
    public MyDataSource()
    {
        if (HttpContext.Current != null)
        {
            this.ConnectionString = MyCinfig.ConnectionString;
            SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
            CancelSelectOnNullParameter = false;

            this.Updated += new SqlDataSourceStatusEventHandler(NXSDataSource_iudExecuted);
            this.Inserted += new SqlDataSourceStatusEventHandler(NXSDataSource_iudExecuted);
            this.Deleted += new SqlDataSourceStatusEventHandler(NXSDataSource_iudExecuted);
        }
    }
}

for Parameter

public class UserIDParameter : Parameter
{
    public UserIDParameter()
    {
        this.Name = "user_id";
        this.DbType = System.Data.DbType.Int32;
    }

    protected override object Evaluate(System.Web.HttpContext context, System.Web.UI.Control control)
    {
        return GetMyCurrentUserID();
    }
}
Arthur