Object of type 'System.DBNull' cannot be converted to type 'System.DateTime'.

Hi,

How can I pass null values to a system.datetime?

Thanks in advance for the help J

 

Below is my code:

 

Public Class Courses

 

  Public Sub Update_Class(ByVal ClassId As Integer, _

   ByVal StatusId As Integer, _

   ByVal ClassNumber As Integer, _

   ByVal TeacherId As Integer, _

   ByVal AmountofParticipants As Integer, _

   ByVal CourseDate1 As DateTime, _

   ByVal CourseDate2 As DateTime, _

   ByVal CourseDate3 As DateTime)

 

  Dim myConnection As New SqlConnection(ConnectionString)

  Dim myCommand As New SqlCommand("Update_Classes", myConnection)

  myCommand.CommandType = CommandType.StoredProcedure

  myCommand.Parameters.AddWithValue("@ClassId", ClassId)

  myCommand.Parameters.AddWithValue("@ClassNumber", ClassNumber)

  myCommand.Parameters.AddWithValue("@StatusId", StatusId)

  myCommand.Parameters.AddWithValue("@TeacherId", TeacherId)

  myCommand.Parameters.AddWithValue("@AmountofParticipants", AmountofParticipants)

  myCommand.Parameters.AddWithValue("@CourseDate1", CourseDate1)

  myCommand.Parameters.AddWithValue("@CourseDate2", CourseDate2)

  myCommand.Parameters.AddWithValue("@CourseDate3", CourseDate3)

  myConnection.Open()

  myCommand.ExecuteNonQuery()

  myConnection.Close()

  End Sub

End Class

 

                      Courselist.aspx.vb_____________

Protected Sub Dates_Updating(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs)

Dim objDate1 As Object

Dim objDate2 As Object

Dim objDate3 As Object

Dim date1 As TextBox = FormView1.FindControl("e_Date1")

If (date1.Text <> String.Empty) Then

objDate1 = DateTime.Parse(date1.Text)

Else

objDate1 = System.DBNull.Value

End If

Dim date2 As TextBox = FormView1.FindControl("e_Date2")

If (date2.Text <> String.Empty) Then

objDate2 = DateTime.Parse(date2.Text)

Else

objDate2 = System.DBNull.Value

End If

Dim date3 As TextBox = FormView1.FindControl("e_Date3")

If (date3.Text <> String.Empty) Then

objDate3 = DateTime.Parse(date3.Text)

Else

objDate3 = System.DBNull.Value

End If

e.InputParameters("CourseDate1") = objDate1

e.InputParameters("CourseDate2") = objDate2

e.InputParameters("CourseDate3") = objDate3

End Sub

 

                      Courselist.aspx____________________________

<asp:objectdatasource id="ObjectDataSource1" runat="server" insertmethod="InsertNewClass" onupdating="Dates_Updating"

oninserting="Dates_Inserting" selectmethod="GetClasses" typename="Courses" updatemethod="Update_Class">

<updateparameters>

<asp:parameter name="ClassId" type="Int32" />

<asp:parameter name="StatusId" type="Int32" />

<asp:parameter name="ClassNumber" type="Int32" />

<asp:parameter name="TeacherId" type="Int32" />

<asp:parameter name="AmountofParticipants" type="Int32" />

<asp:parameter name="CourseDate1" type="DateTime"/>

<asp:parameter name="CourseDate2" type="DateTime" />

<asp:parameter name="CourseDate3" type="DateTime" />

</updateparameters>

</asp:objectdatasource>

 

 

0
whisky
12/24/2006 9:32:33 AM
asp.net.object-datasource 16182 articles. 0 followers. Follow

5 Replies
2668 Views

Similar Articles

[PageSpeed] 23

Hi,

Instead of passing null value.check if value is null and pass the dafault date

and check with the default value

Swati

0
Swati
12/24/2006 10:33:11 AM

I tried the following (below in bold) but for some reason ByVal CourseDate2 As DateTime receives todays date and not the minvalue. not sure why the minvalues changes to today's date.

Public Sub Update_Class(ByVal ClassId As Integer, _

ByVal StatusId As Integer, _

ByVal ClassNumber As Integer, _

ByVal TeacherId As Integer, _

ByVal AmountofParticipants As Integer, _

ByVal CourseDate1 As DateTime, _

ByVal CourseDate2 As DateTime, _

ByVal CourseDate3 As DateTime)

  Courselist.aspx.vb_____________

Protected Sub Dates_Updating(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs)

Dim objDate1 As Object

Dim objDate2 As string

Dim objDate3 As Object

Dim date1 As TextBox = FormView1.FindControl("e_Date1")

If (date1.Text <> String.Empty) Then

objDate1 = DateTime.Parse(date1.Text)

Else

objDate1 = System.DBNull.Value

End If

Dim date2 As TextBox = FormView1.FindControl("e_Date2")

If (date2.Text <> String.Empty) Then

objDate2 = DateTime.Parse(date2.Text)

Else

objDate2 =

CType(DateTime.MinValue, DateTime

End If

 

Dim date3 As TextBox = FormView1.FindControl("e_Date3")

If (date3.Text <> String.Empty) Then

objDate3 = DateTime.Parse(date3.Text)

Else

objDate3 = System.DBNull.Value

End If

e.InputParameters("CourseDate1") = objDate1

e.InputParameters("CourseDate2") = objDate2

e.InputParameters("CourseDate3") = objDate3

End Sub

0
whisky
12/24/2006 4:42:37 PM
Hi,
Pls try this:

objDate2 = DateTime.MinValue.ToString()

Hope it helps.
0
Raymond
12/25/2006 5:13:42 AM

hi,

 now in the

Update_Class coursedate2 gets the value 12:00:00. it's driving me nuts.

_

0
whisky
12/25/2006 6:30:23 AM
What about this one:
Dim objDate2 As String = DateTime.MinValue.ToString("yyyy-MM-dd hh:mm:ss")
Response.Write(objDate2)

0
Raymond
12/25/2006 8:46:29 AM
Reply:

Similar Artilces:

Object of type 'System.DBNull' cannot be converted to type 'System.String' from formview
I have a formview that I am using for input to my datatable.  I am using an objectdatasource that uses a BLL.  This in turn points to my XSD tableadapter. When I test, and only fill in the required fields, I am getting the error Object of type 'System.DBNull' cannot be converted to type 'System.String' I am not sure what field is having the issue.  In my BLL for the Add, I have checked the dates for null and placed the value of DateTime.MinValue.  For the text fields, I have place "" if they are null.  Is this error being create prior to...

System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'
Hello , I am a data table with null able Columns.I am not able to catch the value for DBNull. I did set a value to be returned as empty in dataset. Here is the code where i am trying to catch.1 public List GetStores() 2 { 3 List tmpStore = new List(); 4 StoresService.shopDBDataSet.StoresDataTable stores = 5 storesService.GetStores(); 6 for (int i = 0; i < stores.Count ; i++) 7 { 8 Store store = new Store(); 9 store.StoreID = stores[i].StoreID...

Object of type 'System.Boolean' cannot be converted to type 'System.Int16'.
I have a Checkbox on a detailsview bound to a small int field in my database table.It binds fine when showing the item template on the detailsview, but when I try do an insert/update it blows up with that error.Surely I can bind a checkbox to an integer field, so that unchecked translates to 0, and checked translates to 1?The reason I use a smallint, is its future-proof.  If we decided to switch to a status instead off/on in the future, there are less changes required.  As long as 0 is False and any other value is considered True you should be able to bind the value like this: C...

Unable to cast object of type 'System.DBNull' to type 'System.String'.
Hi i have the following which is meant to check if an image is available and if it isnt it should show a default image however, it gives me this error whenever the image is null in the database. This is the function which returns the error;protected string FormatImageUrl(object url) {if ((url != DBNull.Value) && (url != null) && (url.ToString().Trim().Length > 0)) { //if (File.Exists(url))return (url.ToString().Trim()); } else {return "~/images/default.gif"; } } Hey,Doesn't seem to pass the dbnull check.  Try to do:  if ((!DBNull.Valu...

Unable to cast object of type 'System.DBNull' to type 'System.String'.
Hi there, If I add a new event and leave the description field empty the StarterKit seems to throw a big wobbly over the null values in my DB (SQL2000). Have I made a mistake in the installation or something? The problem lies in this line: SharedRoutines.truncate((string)Eval("description")) because description is Null. I want to sort this in this line and I don't want to reply on validators? This is a known issue reported in the Identified Bugs and Fixes for the ClubSite Starter Kit post. I don't think there is one definitive answer. Search on your error message and you'll fin...

Unable to cast object of type 'System.DBNull' to type 'System.String'
I have received the following error when I am logged in as administrator.  I wanted to test the load event feature.  Allows me to load and save new event.  I can view event on location view page but when I try to link to "list or calender" view I receive this error "Unable to cast object of type 'System.DBNull' to type 'System.String'.  Debug highlights the <asp:HyperlinkID as errored line. Any assistance would be greatly appreciated Brian   <DayEventTemplate> <div style="padding: 3px;"> <asp:Hyper...

Unable to cast object of type 'System.DBNull' to type 'System.String'.
Hi All, I am using Sql server 2005 and VS 2005 C#. Can anybody explain howz solve this prob? Unable to cast object of type 'System.DBNull' to type 'System.String'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.Source Error: Line 3496: get { Line 3497: try { Line 3498: ...

Unable to cast object of type 'System.DBNull' to type 'System.String'.
Hi i get the following error, Unable to cast object of type 'System.DBNull' to type 'System.String'.  A generic image is meant to appear when no image is found in the database for a particular product, this is how the code is laid out;protected string FormatImageUrl(string url) {if (url != null && url.Length > 0) { //if (File.Exists(url))return (url); } else {return "~/Images/CompanyProducts/No_image.jpg"; } }   And the html is like this; <asp:Image ID="Image1" ImageUrl='<%# (string) FormatImageUrl( (string) Ev...

Unable to cast object of type 'System.DateTime' to type 'System.String'.
 Hi,      I got this field (dateSubmitted) having a data type of DateTime but I receive this error "Unable to cast object of type 'System.DateTime' to type 'System.String'."       All value for dateSubmitted field are 12/27/2007 12:00:00 AM. cheers,imperialx  imperialx:I got this field (dateSubmitted) having a data type of DateTime but I receive this error "Unable to cast object of type 'System.DateTime' to type 'System.String'." Can you explain it further as to, ...

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
I'm getting Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'. anyone knows how to write some code to check if my binary data is null then continue check another data? I've tried but seems that byte and string can't convert...  (hope someone can provide me code in VB and where to insert them :))  Here's my code:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim ID As Integer = Convert.ToInt32(Request.QueryString("ID"))Using myConnection As New SqlConnection(Configuratio...

Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.
Hi, I have developed a custom server control for .NET Framework 2.0. The server control has a property named BinaryData of type byte[]. I marked this property to be data bindable. Now, I have varbinary(Max) type of field in my SQL Database and I have used SQLDataSource and bound this varbinary(Max) field with the property BinaryData (byte[]) of my control. It is working fine as long as the data value is not NULL. Now, In my control, I have handled the NULL value so that no Exception is thrown. Still, when I bind this property using the SQLDataSource, I get Error "Unable to cast object ...

Unable to cast object of type 'System.Object[]' to type 'System.Data.DataView'.
I am tring to retieve a single node from my BLL code which retrieve one instane of Repot object:DataView dv = (DataView)objBoutPrep.Select();I am getting error:Unable to cast object of type 'System.Object[]' to type 'System.Data.DataView'. The BLL code returns onle one object and not a list of objects.Please help Client code: <asp:ObjectDataSource ID="objBoutPrep" runat="server" SelectMethod="GetRByID" UpdateMethod="UpdateT" TypeName="MB.K.BLL.G.Report"> <SelectParameters> <asp:Query...

Update error , "Unable to cast object of type 'System.DBNull' to type 'System.String'" , using DataSet as a datasource
Hi All, The following sample code is for testing dw.UpdateData using dataset as the data source. The data retrieval and display part under Form1_Load works fine. However, inputting some values in a new row and calling dw.UpdateData causes the following error: "Unable to cast object of type 'System.DBNull' to type 'System.String'" The error seems to occur when a datawindow column value has been changed from null value. Does anyone know how to fix the error ? Thank you for your advice. I use DW2.5 (build 8542), VS2005 pro, MS SQL Express. ...

Eventcalender: Unable to cast object of type 'System.DBNull' to type 'System.String'.
 I added an event in the eventcalender. When I try to view the events_calender.aspx I get this error message, what can be wrong??   Server Error in '/bfc_csk' Application. Unable to cast object of type 'System.DBNull' to type 'System.String'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'.Source Err...

Web resources about - Object of type 'System.DBNull' cannot be converted to type 'System.DateTime'. - asp.net.object-datasource

Resources last updated: 1/6/2016 11:57:45 AM