• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

stored procedure not recognizing date

CmdDups.Parameters.Add("@LName", SqlDbType.VarChar, 40);
CmdDups.Parameters.Add("@DOB", SqlDbType.SmallDateTime);
CmdDups.Parameters.Add("@Address", SqlDbType.VarChar, 50);

CmdDups.Parameters["@LName"].Value = LName.Text;
CmdDups.Parameters["@DOB"].Value = TxDOB.Text;
CmdDups.Parameters["@Address"].Value = Address.Text;

I have the above stored procedure.  After updates I pass the values to check if the values exist in another table.  I have a record that I have entered that is in the other table.  I am having problems with the DOB field.  When I type "01/12/1946", the parameter runs okay and if identifies that the entry is already in another table.  If I pass the text value of a textbox or datetimepicker the procedure executes fine, but it does not identify that the record is already in the other table.

Please help.
0
yanci1179
Asked:
yanci1179
  • 4
  • 2
  • 2
  • +1
3 Solutions
 
PoeticAudioCommented:
What if you did:

CmdDups.Parameters["@DOB"].Value = Convert.ToDateTime(TxDOB.Text);

does that do anything?
0
 
yanci1179Author Commented:
no, it just goes right over it.  I don't get any errors but it does not identify a match with the date that is in the database.  It only makes a match if I actually type "01/12/1946"

any other suggestions?  I have no clue what is wrong!!
0
 
yanci1179Author Commented:
this is my stored procedure:


CREATE proc dbo.usp_SEDupCk
(@LName varchar(40),
@DOB smalldatetime,
@Address varchar(50))
AS
SELECT       SE.DtSched, SE.MSI, SE.HH_ID,SE.Barcode , SE.LName, SE.MName, SE.Fname, SE.DOB,
      HH.Address, HH.City, HH.Zip,HH.HmNum, HH.OtherNum
            
      FROM       tblHH HH, tblSE SE
      
      WHERE
            (SE.HH_ID = HH.HH_ID AND (lname LIKE '%' + @lname + '%' AND DOB = @dob))
            OR (address LIKE '%' + @address + '%' AND SE.HH_ID = HH.HH_ID)
GO
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
illusioCommented:
Comparing a datetime in SQL is always a risky business even if you use the smalldatetime.
The trouble is that the datetime is not exactly the same (differs in hours and minutes - no seconds with smalldatetime).

Solution:
1) In code:
CmdDups.Parameters["@DOB"].Value = DateTime.ParseExact(TxDOB.Text, "dd/MM/yyyy", new System.Globalisation.DateTimeFormatInfo()).Today;
The Today strips all kind of hour and minute information from the Datetime.

2) In the Stored Proc:
Change: ... DOB = @dob ...
Into: ... DATEDIFF(d, DOB, @dob) = 0 ...

Hope this helps,
Peter
0
 
praneethaCommented:
change the SP to


AND DOB = @dob)

to


convert(datetime,convert(char(10),<DOB >,101)=@dob
0
 
praneethaCommented:
i mean convert(datetime,convert(char(10),DOB,101)=@dob

if that doesnt work try this

convert(datetime,convert(char(10),DOB,101)=convert(datetime,convert(char(10),@dob,101)
0
 
yanci1179Author Commented:
Thanks for the solutions, I'm going to try each one, one at a time.

illusio,

I tried:

DateTime.ParseExact(TxDOB.Text, "dd/MM/yyyy", new System.Globalization.DateTimeFormatInfo()).Today;

and I got the following error:

Static member 'System.DateTime.Today.get' cannot be accessed with an instance reference; qualify it with a type name instead      
0
 
yanci1179Author Commented:
Well, I hate to say this but I tried my original code today and it worked!!??

I'm not sure what happened, I guess I'm going to have to test it a few more time to make sure that it is working.  

thanks again for the help.
0
 
illusioCommented:
Correction:

DateTime.ParseExact(TxDOB.Text, "dd/MM/yyyy", new System.Globalization.DateTimeFormatInfo()).Date;

Must have been half sleeping when I typed this...

My appologies,
Peter
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now