0

I am coding a filter search functionality. that is, user will search data on the basis of any record either datetime or name, number.

here I am also returning the data from other table. So i have implemented a inner join too. For this purpose my store procedure is as follows-

ALTER PROC [dbo].[spFilterSearch]

@var1 VARCHAR(10),
@var2 VARCHAR(10),
@Printdate DATETIME,
@ToDate varchar(10),
@FromDate varchar(10)

AS BEGIN
    DECLARE @td DateTime SET @td=CONVERT(DATETIME,@ToDate,105)
    DECLARE @fd DateTime SET @fd= CONVERT(DATETIME,@FromDate,105)
    if(@Printdate <> NULL)
        BEGIN
            SELECT cVar1, cVar2, Printdate,
            bg.Fname AS Name
            FROM dbo.Table1 
            INNER JOIN dbo.jTable2 jt ON Table1.id = jt.id
            WHERE(cVar1 LIKE @var1+'%') AND
            (cVar2 LIKE @var2+'%') 
            AND (convert(datetime,Printdate,105)=@FPdate)
        END
    ELSE
        BEGIN
            SELECT cVar1, cVar2, Printdate,
            bg.Fname AS Name
            FROM dbo.Table1 
            INNER JOIN dbo.jTable2 jt ON Table1.id = jt.id
            WHERE(cVar1 LIKE @var1+'%') AND
            (cVar2 LIKE @var2+'%') 
            AND (convert(DATETIME,Printdate,105)>=@fd AND convert(DATETIME,Printdate,105)<=@td)

        END

END

(this procedure don't return any record) At front end I have coded like, to send null date I have created FPdate property as below public DateTime? FPdate { get; set; }

and in this way I am using it in search event handler

if (txtPrintedOn.Text != "")
          {
              string[] dtt = txtPrintedOn.Text.Split(' ');
              objprop.FPdate = Convert.ToDateTime(dtt);
          }
          else
          {
              objprop.FPdate = null;
          }

and to send it in the form of parameter from the business layer I have wrote code like this-

if (prop.FPdate.Equals(null))
        {
            cmd.Parameters.AddWithValue("@Printdate", SqlDbType.DateTime).Value = Convert.DBNull;
        }
        else
        {
            cmd.Parameters.Add("@Printdate", SqlDbType.DateTime).Value = prop.FPdate;
        }
if (prop.ToDate != null)
            cmd.Parameters.AddWithValue("@ToDate", SqlDbType.VarChar).Value = prop.ToDate;
        else cmd.Parameters.Add("@ToDate", SqlDbType.VarChar).Value = Convert.DBNull;

At the end Data set is being return empty. I am banging my head against the wall since many hours but not getting any clue

where I am going wrong. Please suggest a way to make it run.

Thanks

Nikhil G
  • 1,536
  • 3
  • 13
  • 28

1 Answers1

0

Change SP to use following

if(@Printdate is not NULL)

Checkout these links -

Is there any difference between IS NULL and =NULL

SQL is null and = null

Use this

cmd.Parameters.Add("@FPdate", SqlDbType.DateTime).Value = prop.FPdate.Value;

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48