Solved

Manual Insert Fails

Posted on 2008-10-09
6
165 Views
Last Modified: 2010-04-21
I'm trying to insert a record to a sql server using an objectdatasource but I'm getting an exception when it gets to inserting the date field. The exception is:
"Object reference not set to an instance of an object."

The variable has the correct date in it so I'm not really sure what this means. I will post a copy of all the code I'm using in case someone can see what I'm doing wrong. Thanks for any help on this.

Code Behind on the Onclick event:
 
        Dim strWorkOrderID As String = txtWorkOrderID.Text
        Dim datDateWorked As Date = txtDateWorked.Text
        Dim strWorkPerformed As String = txtWorkDone.Text
       
        odsWorkOrderDetail.InsertParameters("WorkOrderID").DefaultValue = strWorkOrderID.ToString()
        odsWorkOrderDetail.InsertParameters("WorkDone").DefaultValue = strWorkPerformed.ToString()
        odsWorkOrderDetail.InsertParameters("DateWorked").DefaultValue = datDateWorked.ToShortDateString()  ****Fails Here****
        odsWorkOrderDetail.Insert()
 
 
 
ObjectDataSource:
 
 
 
<asp:ObjectDataSource ID="odsWorkOrderDetail" runat="server" 
                    InsertMethod="AddWorkOrderDetail" OldValuesParameterFormatString="{0}" 
                    SelectMethod="GetWorkOrderDetails" TypeName="WorkOrderBLL">
                    <SelectParameters>
                        <asp:Parameter Name="WorkOrderID" Type="String" />
                    </SelectParameters>
                    <InsertParameters>
                        <asp:Parameter Name="WorkOrderID" Type="String" />
                        <asp:Parameter Name="WorkDate" Type="DateTime" />
                        <asp:Parameter Name="WorkDone" Type="String" />
                    </InsertParameters>
                </asp:ObjectDataSource>
 
 BLL:
 
 <System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert, False)> _
   Public Function AddWorkOrderDetail _
                  (ByVal workOrderID As String, _
                   ByVal workDate As Date, _
                   ByVal workDone As String) As Boolean
        Dim workOrderDetail As New WorkOrderDAL.FAC_WorkOrderDetailsDataTable
        Dim workOrderDetailRecord As WorkOrderDAL.FAC_WorkOrderDetailsRow = workOrderDetail.NewFAC_WorkOrderDetailsRow()
 
        workOrderDetailRecord.WorkOrderID = workOrderID
        workOrderDetailRecord.WorkDate = workDate
        workOrderDetailRecord.WorkDone = workDone
 
        workOrderDetail.AddFAC_WorkOrderDetailsRow(workOrderDetailRecord)
        Dim rowsAffected As Integer = DetailsAdapter.Update(workOrderDetail)
        Return rowsAffected = 1
    End Function
 
 
 
 
Stored Procedure:
 
 
 
ALTER PROCEDURE dbo.spInsertFAC_WorkOrderDetails
(
@WorkOrderID varchar(15),
@WorkDate datetime,
@WorkDone varchar (1000))
AS 
SET NOCOUNT OFF;
 
 
INSERT INTO dbo.FAC_WorkOrderDetails ([WorkOrderID], [WorkDate], [WorkDone])
     VALUES (@WorkOrderID, @WorkDate, @WorkDone);
SELECT WorkOrderID, WorkDate, WorkDone FROM dbo.FAC__WorkOrderDetails 
WHERE (WorkOrderDetailID = SCOPE_IDENTITY())

Open in new window

0
Comment
Question by:AkAlan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:fanopoe
ID: 22680523
dates really stink in cross platform programming, and you're taking a test string, changing it to a data, then to a date string, then passing it to a database. What happens when you declare datDateWorked  as a string instead and pass the value thusly?

hth
0
 
LVL 6

Author Comment

by:AkAlan
ID: 22680565
Same exact error. I even tried to hard code the date like this:
odsWorkOrderDetail.InsertParameters("DateWorked").DefaultValue = #1/1/2008#
it still gave me the same exact error
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 22680635
Parameter name does not match?
<asp:Parameter Name="WorkDate" Type="DateTime" />
odsWorkOrderDetail.InsertParameters("DateWorked").DefaultValue = datDateWorked.ToShortDateString()  
0
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 6

Author Comment

by:AkAlan
ID: 22680651
Thanks, I must be way too close to this.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22680705
It happens. Especially after you have seen your own code for a thousand times, you will need a pair eyes from another person to find this kind of error.
0
 
LVL 6

Author Closing Comment

by:AkAlan
ID: 31504744
That did the trick. Now I have another problem but that will be for another post if I can't figure it out. Thanks, I looked at this for 4 hours.
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
In this article I will describe the Backup & Restore 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.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

724 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