Solved

SessionParameter DefaultValue DBNull

Posted on 2007-11-13
6
1,347 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

862 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

23 Experts available now in Live!

Get 1:1 Help Now