Avatar of mmedi005
mmedi005
 asked on

How do i pass a NULL value in a SqlDataSource?

How do i pass a NULL value in a SqlDataSource?

My stored procedure has a IF statement where it passes a value if @LastName has a string, but if its empty, @LastName is NULL and SELECT statement executes without the condition.

I want it so that the user enters nothing, it passes NULL, but if the user enters text, the text passes to the @LastName parameter.  

When I enter something in the textbox it works, but if I leave it blank it does not execute the SELECT statement without the conidition.

I tried using the code below....

Any ideas?

Stored Procedure:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_StoredProcedure]
      @LastName                  varchar(50)      = NULL
AS
BEGIN
      SET NOCOUNT ON;

      DECLARE @sql            nvarchar(4000),
                  @paramlist      nvarchar(4000)      

      SET @sql = 'SELECT  e.FirstName, e.LastName FROM Employees'

      IF @LastName IS NOT NULL
      BEGIN
         SET @sql = @sql + N' AND e.LastName LIKE @xLastName + ''%'''
      END

      SET @paramlist = '@xLastName varchar(50)'

      EXEC sp_executesql @sql, @paramlist, @LastName
END
if(uxLastNameTextbox.Text != "")
      uxEmployeeSqlDataSource.SelectParameters["LastName"].DefaultValue = uxLastNameTextbox.Text;
else
      uxEmployeeSqlDataSource.SelectParameters["LastName"].DefaultValue = null;

Open in new window

.NET ProgrammingMicrosoft SQL ServerASP.NET

Avatar of undefined
Last Comment
mmedi005

8/22/2022 - Mon
nmarun

Why not the below? This way you don't need a dynamic query.

IF @LastName IS NOT NULL
BEGIN
     SET @LastName = @LastName + '%'
     SELECT  e.FirstName, e.LastName FROM Employees e WHERE e.LastName LIKE @LastName
END
ELSE
     SELECT  e.FirstName, e.LastName FROM Employees e
END

Open in new window

burakiewicz

have you tried
uxEmployeeSqlDataSource.SelectParameters["LastName"].DefaultValue = DBNull.Value.ToString();
ASKER CERTIFIED SOLUTION
Lee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
mmedi005

ASKER
>>Why not the below? This way you don't need a dynamic query.

Because there is more to the sql statement, but if i can get this small part to work then the rest will work as well...

>>have you tried &
>>Use DBNull instead of null.

yes, and it does not work....i tried DBNull.Value & DBNull.Value.ToString()

any other ideas?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
jabcoco

Check the 2 last post.

Ref:
http://forums.asp.net/t/963932.aspx
//2 possible solutions
 
<asp:SqlDataSource OnSelecting="SqlDataSourceSelectingEventHandler" ... /> 
protected void SqlDataSourceSelectingEventHandler(Object sender, SqlDataSourceSelectingEventArgs e){
  ((SqlCommand)e.Command).Parameters["paramNameHere"].Value = DBNull.Value;
} 
 
//or
<asp:SqlDataSource CancelSelectOnNullParameter="False" />

Open in new window

mmedi005

ASKER
        EnableEventValidation="false"