Solved

SessionParameter DefaultValue DBNull

Posted on 2007-11-13
6
1,361 Views
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?

Thanks,
Aaron
0
Comment
Question by:CoconutTelegraph
  • 3
  • 3
6 Comments
 

Author Comment

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

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

Accepted Solution

by:
Sammy earned 500 total points
ID: 20277515
You need to handle the onSelecting event of the SqlDataSource
example
<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
0
 

Author Comment

by:CoconutTelegraph
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!!
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 27

Expert Comment

by:Sammy
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

http://msdn2.microsoft.com/en-us/library/9a4kyhcx(vs.80).aspx

good luck
Sammy
0
 

Author Comment

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

0
 
LVL 27

Expert Comment

by:Sammy
ID: 20277790
You welcome and thanks for the Points
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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