SessionParameter DefaultValue DBNull

Posted on 2007-11-13
Last Modified: 2008-02-01
Hi there,

This may seem like an easy question but I can't seem to get it to work. I've got a SessionParameter in a SqlDataSource with a SelectCommandType of StoredProcedure. If the SessionParameter is null/nothing I need to pass the stored procedure a value of System.DBNull.Value. How do I do that?

Question by:CoconutTelegraph
  • 3
  • 3

Author Comment

ID: 20277200
btw, I've tried this but it doesn't seem to work....

<asp:SessionParameter Name="ProductID" DefaultValue="" ConvertEmptyStringToNull="true" SessionField="MySessionProductID" />
LVL 27

Accepted Solution

Sammy earned 500 total points
ID: 20277515
You need to handle the onSelecting event of the SqlDataSource
<asp:SessionParameter Name="ProductID" DefaultValue=""ConvertEmptyStringToNull="true" SessionField="MySessionProductID" Type="String"  />
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        if (e.Command.Parameters["@ProductID"].Value==null)
            e.Command.Parameters["@ProductID"].Value = DBNull.Value;

that should take care of it

Author Comment

ID: 20277559
Interesting. Can I ask you from a design perspective, which do you think is cleaner and better code?

1) Using the method you've suggested for setting the values to DBNull.Value when null.

2) Ditching the SqlDataSource, and opting to hit the database directly in the DataBinding event of the GridView and then attaching the returned ADO.Net DataTable to the DataSource property.

Hmmm. I can't decide now. :(

Anyhow thanks!!
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

LVL 27

Expert Comment

ID: 20277727
I a not a fan of the SQLDataSource for a simple reason. It mixes data layer login with the UI layer
If you really have to use a control like the SqlDataSource, I would recommend using the ObjectDataSource.
take a look at the docs here

good luck

Author Comment

ID: 20277738
Good point. Well that certainly gives me something to think about. Thanks for the quick and thorough response Sammy!

LVL 27

Expert Comment

ID: 20277790
You welcome and thanks for the Points

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now