Solved

Linq To SQL Stored Procedure Error When Date Is Empty

Posted on 2011-02-28
4
646 Views
Last Modified: 2013-11-11
I have a SQL insert stored procedure :

USE [RMA]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_Incident_insertTicket]
      -- Add the parameters for the stored procedure here
      @orderid varchar(50) = null,
      @email varchar(50) = null,
      @name varchar(50) = null,
      @origshipdate date = null,
      @reasonid int = null,
      @resolutionid int = null,
      @notes text = null,
      @returntagoption char(10) = null,
      @refundrequired char(10) = null,
      @refundamount float = null,
      @daterefundissued date = null,
      @shipperid int = null,
      @item varchar(50)= null,
      @itemqty int = null,
      @ebaycredit char(10) = null,
      @ebaycreditdate date = null,
      @lostvalue float = null,
      @rmarequested char(10) = null,
      @incidentcreatedby int = null
      
      
      
AS
BEGIN
      SET NOCOUNT ON;
      
      INSERT INTO Incidents
          (    OrderID, Email, Name, OriginalShipDate, IncidentReasonID, IncidentResolutionID, Notes, ReturnTagRequired, RefundRequired, RefundAmount,            DateRefundIssued, ShipperID, InventoryItem, InventoryQuantity, eBayCredit, eBayCreditFileDate, LostValue, RMARequested, IncidentCreatedBy
          )
     VALUES
          (
            @orderid,@email,@name,@origshipdate,@reasonid,@resolutionid,@notes,@returntagoption,@refundrequired,@refundamount,
            @daterefundissued,@shipperid,@item,@itemqty,@ebaycredit,@ebaycreditdate,@lostvalue,@rmarequested,@incidentcreatedby                
          )


      
      
END

Now I've set all the variables in the stored procedure to null and all values in the table can be null. This will work if I fill all the controls in with a value. The problem is if one of the options to, lets say i don't want to create a refund, there would not be a refund date. So that date field would be empty, and when exexuted it throws an error. How can I pass in the linq statement with null date values.

RMADataClassDataContext CreateTicket = new RMADataClassDataContext();

           CreateTicket.sp_Incident_insertTicket(TextBox2.Text, TextBox3.Text, TextBox4.Text, Convert.ToDateTime(TextBox5.Text), int.Parse(DropDownList1.SelectedValue), int.Parse(DropDownList2.SelectedValue), TextBox6.Text, DropDownList3.SelectedValue, DropDownList4.SelectedValue, float.Parse(TextBox7.Text), Convert.ToDateTime(TextBox8.Text), int.Parse(DropDownList5.SelectedValue),  DropDownList6.SelectedValue, int.Parse(TextBox9.Text), DropDownList7.SelectedValue, Convert.ToDateTime(TextBox10.Text), float.Parse(TextBox1.Text), DropDownList8.SelectedValue, int.Parse(DropDownList9.SelectedValue));
0
Comment
Question by:frtools
  • 2
4 Comments
 
LVL 6

Expert Comment

by:anushahanna
ID: 35003633
use ISNULL function to provide your replacement value, so that there is no erroring out
http://msdn.microsoft.com/en-us/library/ms184325.aspx
0
 

Author Comment

by:frtools
ID: 35007670
I'm sure that the SQL S_P is not the problem when I execute the S_P and do not apply any values and do not check the pass null value it works with no errors. The problem seems to be in the Linq statement itself.
0
 
LVL 12

Accepted Solution

by:
kumar754 earned 500 total points
ID: 35021025
This might be because you are not using Nullable Types, Linq is expecting a nullable type as a parameter of the stored procedure.

Here in the "Convert.ToDateTime(TextBox5.Text)" if the date is null. it will not convert to a nullable datetime
so you shld rather convert it to similar type and then pass the variable. Same goes for int, decimal data types..

you shld rather use like this:

DateTime? _DateRefundIssued= null;

DateTime tempDate;
if (DateTime.TryParse(TextBox5.Text, tempDate))
  _DateRefundIssued = tempDate; // assign only if the date value exist or in proper format

// else it will be null..

Now pass you _DateRefundIssued variable to the Store Procedure as a parameter.. it shld work.

same goes with int and decimal too..

so:

int? _OrderID = null;

int tempOrderID = 0;
if (int.TryParse(TextBox2.Text, tempOrderID))
   _OrderID = tempOrderID;

Now, pass this to your stored procedure:

RMADataClassDataContext CreateTicket = new RMADataClassDataContext();

CreateTicket.sp_Incident_insertTicket( _OrderID , <nullable_type> , <nullable_type>, _DateRefundIssued, <nullable_type>, <nullable_type>, etc...);



0
 

Author Comment

by:frtools
ID: 35027546
Perfect works exactly like I want it to.

the tryparse statement needs an out result:

if (DateTime.TryParse(TextBox5.Text, out tempDate))
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

20 Experts available now in Live!

Get 1:1 Help Now