Insert Function - Object Cannot Be Cast From DBNull To Other Types
Nov 9, 2011
I am having problems with my code, where I get the "Object cannot be cast from DBNull to other types" message when Insert(). I get the message on every Insert to this table, even though the data is successfully inserted. If I Update the same data after Insert(), it does so without error. I have checked my values and none of them are DBNull. Even when the same values are passed on Insert that are passed on Update.
My code is :
Private Sub Save()
Dim bo As tbl_GL
bo = New tbl_GL()
bo.ConnectionString = AppConfig.ConnectString
[Code] .....
View 4 Replies
ADVERTISEMENT
May 1, 2009
how can I troubleshoot "Object cannot be cast from DBNull to other types."?I am using a combobox, to retrieve the price and discount price of the service, If I select an item in combobox3 once, error will not occur, but if I select a item again in combobox3, "Object cannot be cast from DBNull to other types." will occur..here is my code
Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
Dim ds As New DataSet
[code]....
View 6 Replies
Jun 20, 2012
how can I troubleshoot "Object cannot be cast from DBNull to other types."?I am using a combobox, to retrieve the price and discount price of the service, If I select an item in combobox3 once, error will not occur, but if I select a item again in combobox3, "Object cannot be cast from DBNull to other types." will occur..
here is my code
Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
[code].....
View 4 Replies
Dec 24, 2009
Now I want to display picture in picturebox1.To do this I use this codes, but it does not display picture
on this line: Dim arrPicture() As Byte = CType(dt2.Rows(0)("img_path"), Byte())
it shows this message,
Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.[code]......
View 1 Replies
Dec 14, 2011
I have a generic Class I'm using to hold information loaded from a database.I have a method which takes a DataRow as an argument, uses the object's known column name and extracts the data from the DataRow, such that:Dim loadData As T = CType(myDataRow("myColumnName"), T))works as my default assignment in most cases.Unfortunately, due to some horrifying design constraints, some of my columns may be null, and may also be taken from enumerations.This means that when <T> is Nullable(Of SomeEnumeration) the above code does not work because I can't cast 0 directly to SomeEnumeration.Zero.Is there some way to check whether <T> is Nullable(Of [Enum])? Or some way to write a method which allows Integers to be cast to Nullable(Of [Enum])?I feel like I'm forgetting something that would allow me to write one of the other of these, but my weak google-fu is turning up nothing.
EDIT: Okay, thanks to dasblinkenlight's answer below, I can detect when this circumstance is occurring, but what I need to do now is to take a type <T> which I know is Nullable(Of SomeClass), get a type reference to SomeClass and then create a new object of type Nullable(Of SomeClass) and assign that to LoadData.My problem was that I had a lot of difficulty in finding any function which would accept baseType as an actual Type.Parse accepted baseType as a parameter, I knew baseType was an [Enum] type because of dasblinkenlight's code, so I was, in this instance, able to code a solution. It's a solution which is very specific to my problem (i.e., T is Nullable(of SomeEnumeration)), but it's a solution nonetheless.
View 2 Replies
May 22, 2012
I converted the Microsoft's datagridviewcustomcolumn sample into vb.net to host maskedtextbox control. The maskedtextbox column works fine when it is not bound to database field. But it raises exception when it is bound. I have shaded the line that raises the exception.
[Code].....
View 11 Replies
Jun 30, 2010
I've spent the last three day on this. Hope someone can shed some light on the problem.I have a datagridview I which to total a column and place in a textbox. with the coding below I do get the first number and it is place correctly in the textbox but when I complete the data row and move to the next, I receive the following error. "object cannot be cast from DBNull to other type." Read and tried a lot of ideas on line , could not get any of them to work.
[Code]...
Failure is not an option! But sometime you need to look at those option.
View 7 Replies
Jan 10, 2012
Is there a technical reason why there is no implicit conversion from DBNull to the various nullable and/or sql types? I understand why the conversions don't currently happen, but don't understand why an implicit conversion wasn't created at the time or added in subsequent versions of the framework.Just to be clear, I'm looking for technical reasons, not "because that's they way they did it" or "I like it that way".
View 2 Replies
Apr 5, 2012
Ran into this error message while trying to select some records off a table.[code]...
View 2 Replies
Nov 1, 2009
I have a class with a Property called 'Value' which is of type Object.Value can be of any type, a structure, a class, an array, IList etc.My problem is with the setter and determining whether the value has changed or not.This is simple enough for value types, but reference types and lists present a problem.For a class, would you assume that the Equals method has been implemented correctly, or just assume that the value has changed every time the setter is called?If I did assume it's changed, then perhaps I should assume it for value types as well, so that the behaviour is consistent.
View 2 Replies
Mar 8, 2009
Could anyone assist me in solving my problem ? My code was written in VB (VS 2003.)I got this error: System.InvalidCastException: Cast from type 'DBNull' to type 'Integer' is not valid. in my code.Here is the code line where I am getting the error: LeadCampusID = CInt(.Item("mkCampusID"))I fixed the error by making the changes in the code as :
If Not IsDBNull(.Item("mkCampusID")) Then
LeadCampusID = CInt(.Item("mkCampusID"))
Else
[code].....
View 1 Replies
Jun 22, 2010
I use VB .NET 2002 (academic version) I have created an MS Access database to be populated with vaules extracted from dBase files. Some numerical values can be null - ie where no data was recorded in the dBase file. Relevant field properties in MS Access mdb table are set to "Not Required" as follows - dt0 is the name in code of the Access table "AllData":
With dt0.Columns
.Append("Date", DataTypeEnum.adDate)
colTest.Name = "Ch1Deg"
[CODE]...
Using an INSERT INTO command I can send a null value to the field:
SQLMdb = "INSERT INTO [AllData]" " ([Date],[Ch1Deg])" & _
" VALUES ('" & dDate1 & "',null)
My problem is how to pass a variable which sometimes takes the value System.DBNull.
If I use SQLMdb = "INSERT INTO [AllData]" " ([Date],[Ch1Deg])" & _
" VALUES ('" & dDate1 & "','" & dDeg & "')
Then when dDeg is numeric then there is no problem and the table is populated. When data is missing and dDeg takes the value System.DBNull then it's a no go! The error message is "Data type mismatch in criteria expression."
Is there a way round this with VB .NET 2002 or do I need to upgrade to a later version which has nullable data types, eg nullable integers and doubles etc?
View 3 Replies
Feb 9, 2012
Private
Sub cmdExit_Click(ByVal
sender As System.Object,
ByVal e
[code]....
View 3 Replies
Feb 22, 2012
I have
Public stack() As CTest
I want
Public stack() As Object
The latter is giving the error "Unable to cast object of type 'Object' to 'CTest'." when used:
Dim thestack As CTest() = testdatabase.getStack
Where testdatabase.getStack simply returns stack();
Public Function getStack() As Object()
Return stack
End Function
This fixes it, but it's not ideal (for me personally):
Dim thestack As Object() = testdatabase.getStack
So if I could keep the variable as-is (Public stack() As Object) and then do something along the lines of class.stack() = CTest I should be able to do Dim thestack As CTest() = testdatabase.getStack because the object array will programmatically have changed from Object to CTest.
View 1 Replies
Aug 19, 2011
In C# we can do something like this:
Honda a = new Car(); and that works but the same one doesn't work in Visual Basic.NET (I am fairly new to Visual Basic)
Dim a as Honda = new Car and it says Unable to cast object of type 'SampleApp.Car' to type 'SampleApp.Honda'.
Here is my sample code:
Module Module1
Sub Main()
Dim a As B = New A
[Code]....
View 3 Replies
Apr 23, 2012
I've got a little function that just changes empty and null strings to "null":
Public Function CatchString(ByVal stringValue As String) As String
If stringValue = "" Or stringValue Is Nothing Or stringValue Is DBNull.Value Then
Return "null"[code].....
I added the bit about DBNull because sometimes it gets null values from a database. This doesn't work, though, because it won't take DBNull as a parameter.I tried changing the stringValue parameter from a string to an object, but then the If statement gives this error:
Quote: Object reference not set to an instance of an object.How do I make the function accept DBNull as a parameter?
View 3 Replies
Mar 29, 2010
I am having some trouble creating an object array of different object types. Here is what I have so far:
Public Sub New()
Dim b1 As New Book("Book Title 1", "Gold Book Publisher", 1.0, "Bob Jones", "192DJDJD212", "01/02/10")
Dim b2 As New Book("Book Title 2", "Gold Book Publisher", 1.0, "Bob Jones", "192DJDJD212", "01/02/10")
Dim b3 As New Book("Book Title 3", "Gold Book Publisher", 1.0, "Bob Jones", "192DJDJD212", "01/02/10")
[code]....
If I define the array As Book (Public publicationArray(6) As Book) or Magazine (Public publicationArray(6) As Magazine) I am able to access the properties of that class but not both. I think I need to define a type for each array instance but not sure how to do this.
View 5 Replies
Apr 30, 2009
I've got a f(x) that returns a collection of user controls. .Net lets me just treat the f(x) name as the collection.
ex)
Private Function GetCcB() As Collection(Of Reports_ucColumn)
Dim cc As New Collection(Of Reports_ucColumn)
cc.Add(Me.ucColumn0)
[code].....
View 3 Replies
Mar 10, 2011
ok I have a weird issue. I created a master record in the master table and then a record in the detail table. when creating the detail record i get a Specified cast is not valid error. Now another weird thing is that once I create the master record I can't query and verify it was created. But when i got to sql management studio I can see the master record. Below is my two procedures and the table definitions. Master table primary key is INVNUM child table primary key it INVLIN_ID and the reference key is INVNUM
View 13 Replies
Mar 1, 2012
I have a method which receives an object as parameter. This object is always of a class I have created, it will never be something simple as string, integer, etc
In my simple example I have created a class called ClassThatWillBeUpdated that requires a type for some reason not shown in the example
Public Class ClassThatWillBeUpdated(Of T)
Private _count As Integer = 0
Public Property Count() As Integer
[Code]....
Or maybe it would be possible to call the DoWork(of T)(item) directly when creating the thread, but as far as I know it's not possible to make a thread run a method that has (of T) as that is basically what I'm looking for
Needless to say, the above code would fail during compile as I have no idea how to do it, if it's even possible. I'm using .net framework 3.5
View 1 Replies
Mar 9, 2012
All I am trying to do is select one school id. Do you know where I went wrong? ty
Public Function findId(ByVal table_name As String, ByVal name As String)
Try
con.ConnectionString = connection_String
[Code]......
View 2 Replies
Mar 24, 2011
unable to cast object of type dataset to type datatable.Dim dataTable As DataTable = DirectCast(dataGrid.DataSource, DataTable)
View 4 Replies
Feb 22, 2011
I using Microsoft.Office.Interop.Excel I get returned a 2D array of type object[,] which contains double for elements. Note that the index lower bound is 1 instead of the default 0, but I can deal with that easily. How can nicely convert the array into double[,] using .NET 3.5. (by nicely I mean concise, or compact).
[Code]...
View 3 Replies
Jul 24, 2011
Is this code you have obtained from elsewhere? Have you tried debugging it line by line? Which line throws the error?
View 2 Replies
Jan 14, 2011
I am new in programming with VS2008. Nowdays I try to built a simulation of graphics movement. I use Panel to draw the graphics but I faced a problem when I want to move the object. There were a message
"Unable to cast object of type 'System.EventArgs' to type 'System.Windows.Forms.PaintEventArgs'.".
Public Class Form1
Dim x, y As Integer
Dim n As Integer
Dim xp, yp As Integer
Dim xt, yt As Integer
[Code] .....
View 9 Replies
May 20, 2009
In the code base I'm working in there have a method that has the signature
Public Sub SetDropDownValue(Of T As Structure)(ByVal target As ListControl, ByVal value As Nullable(Of T))
The method I am writing is passed a parameter of type object.
How can I cast the object into something that can be passed into the SetDropDownValue method?
View 2 Replies
Jan 25, 2012
I am using reflection to get an object's type, or for this issue an object that has instance properties type, at runtime and then I need to change an existing variable's type into that newly found type. Is this possible? For example, the following code does not work in the line indicated within:
Public Sub DoSomething(x As T, y As T, exp As String)
'This is a instance property on the object of a different type
'i.e. 'T.AnotherType' We need to reflect to find out what type of object
[code].....
View 1 Replies
Mar 14, 2012
How to cast Datagridview.selectedRow(0).databounditem in Object Classes as we do in C#[code]...
View 2 Replies
Oct 7, 2008
i am trying to create an sql parameter but i am having an error. it gives me an error like this: Unable to cast object of type 'System.Boolean' to type 'System.Data.SqlClient.SqlParameter'. on all of this line
Dim paramcollection As New List(Of SqlParameter) ' no error on this part but the lines below gives me an error:
paramcollection.Add(New SqlParameter("@CGLCode", SqlDbType.VarChar).Value =
[Code].....
View 4 Replies
Jul 22, 2011
I have do the coding to export the data from DataGridView to Excel. But If am running the code am getting the below error.
[Code]...
View 4 Replies