Null value to a Stored procedure



Hi Experts,
I have written  Stored Proc where I get the Site Id and display the records accordingly.
Now I want if 'All' is selected then all the records should be displayed.

Create PROCEDURE pro_SiteWiseResourceSkillFilter                
@domainId varchar(200),                        
@domainTechId varchar(200),                
@skillId varchar(200),  
@siteId int                        
as                                    
BEGIN                                    
                           
SET NOCOUNT ON      
 select distinct Us.WduserId,EmpName as ResourceName                
  from UserSkills Us inner join vw_getusers on Us.WduserId = vw_getUsers.WduserId                            
  inner join Domain d on d.DomainId = Us.DomainId and d.IsDeleted =0                          
  inner join Technologies Dt on Dt.TechId =Us.domainTechId and Dt.IsDeleted =0                              
  inner join Skills sk on sk.SkillId =Us.SkillId and sk.IsDeleted =0                          
  where vw_getUsers.DesignationId not in (2,25,28,38) and Us.IsActive =1  and vw_getUsers.SiteId = Coalesce(@siteId,vw_getUsers.SiteId)
  order by EmpName        
           
End            
                           
SET NOCOUNT OFF                                            
                           
END            
   
 
I am using Coalesce for this purpose.
But from my page how should I pass null value to the SP.

   public int SiteId_;

 protected void ddlSites_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSites.SelectedIndex == 0)
            {
             
               // SiteId_ = DBNull.Value; didn't work
            }

            BindSiteWiseResourceList(string.Empty, string.Empty, string.Empty, SiteId_);
        }

How can I pass null.
LVL 8
Johny BravoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

krunal_shahCommented:
check this it will help,
http://forums.asp.net/p/1132283/1798468.aspx

i think this should  work,
SiteId_ = DBNull.Value;
is it giving any error?
0
PryratesCommented:
you should use DBNull.Value as parameter of you stored proc.
That should do.
I created a simple sample that you might give a try.
Of course you need to replace the placeholder with your correct settings
SqlConnection conn = new SqlConnection(<CONNECTIONSTRING>);
            SqlCommand sqlCom = new SqlCommand("testNull", conn);
            sqlCom.CommandType = System.Data.CommandType.StoredProcedure;
            SqlParameter param = new SqlParameter("siteid", System.Data.SqlDbType.Int);
            param.Value = DBNull.Value;
            sqlCom.Parameters.Add(param);
            conn.Open();

           SqlDataReader rdr = sqlCom.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
           if (rdr.HasRows)
           {
               while (rdr.Read())
               {
                   Console.WriteLine(rdr.GetString(0)); //assumes first element of result is a string
               }
           }
           rdr.Close();
           conn.Close();

           Console.ReadKey();


//Stored proc:

CREATE PROCEDURE testNull
@siteid int
as
BEGIN
SELECT * FROM <TABLE>
END

Open in new window

0
vora_bhaumikCommented:
Try like this,


 public int? SiteId_ =null;

 protected void ddlSites_SelectedIndexChanged(object  sender, EventArgs e)
        {
            if  (ddlSites.SelectedIndex > 0)
            {
             
              SiteId_ = ddlSites.SelectedValue;
            }

             BindSiteWiseResourceList(string.Empty,  string.Empty, string.Empty, SiteId_);
        }




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

rushShahCommented:
try using ISNULL instead of coalesce...
0
cadsjoCommented:
You can use the default value for the parameter

Create PROCEDURE pro_SiteWiseResourceSkillFilter                
@domainId varchar(200),                        
@domainTechId varchar(200),                
@skillId varchar(200),  
@siteId int = NULL

Now, just don't provide the siteId and in your sp it will be NULL.

HTH
Cadsjo
0
Johny BravoAuthor Commented:
It works fine.
Thanks to all experts who invest their time in this issue.
0
Johny BravoAuthor Commented:
//SiteId_ = DBNull.Value;
As SiteId_ is of type int,it is throwing error.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.