Cast from type 'DBNull' to type 'String' is not valid.

How would I check to make sure my SQL db is not pulling a null value?  I'm using the following code and I get the 'Cast from type 'DBNull' to type 'String' is not valid.' error.


If (dtrUserDetails("D_Address2") Is System.DBNull.Value) Then
_Address2 = ""
Else
_Address2 = dtrUserDetails("D_Address2")
End If

Any help would be much appreciated.
0
BobbyFive
8/2/2003 2:44:03 PM
asp.net.object-datasource 16182 articles. 0 followers. Follow

1 Replies
580 Views

Similar Articles

[PageSpeed] 37

Change your line to 

_Address2=dtrUserDetails("D_Address2") & ""
The empty string in the end will convert the filed to a string value even if it has a <Null> value. HTH
0
sysadmin
8/2/2003 3:40:48 PM
Reply:

Similar Artilces:

Cast from type ''''DBNull'''' to type ''''String'''' is not valid.
How do I avoid getting this error: Cast from type ''''DBNull'''' to type ''''String'''' is not valid. I am trying to get values from the database into a form for updating using a Datareader, SQL is set to allow nulls for certain fields as they are not required but the only way I can get the form to display is by adding a space in SQL. Is there another way around this? Check if it is equal to DBNull.Value first and if it is, don't do the cast.Stanley Tan theSpoke Blog Where in the code does it need to go? I am using the following and it's the profile that isn't always requi...

Cast from type 'DBNull' to type 'String' is not valid
I REALLY need some help, so here goes. The following function does work as long as the "mail" field in the database isn't NULL. As you can see I have tried a couple of ways to check for a NULL value but none of them seem to work. What's supposed to happen is the function checks to see whether or not a user has an email address in the database. If so, then the email address gets returned, if no email address then I need something back from the function so I can go about another way to get the info to the user. Public Function GetReqByEmail(ByVal reqby As String) As String...

Cast from type 'DBNull' to type 'String' is not valid
Hi,  I have an old website and old database (SQL 2000).  I add new column "ImageURL' to database, so all data (several thousand items) are "NULL" (actually is dbNull) under this column.  In searchResult.aspx page, inside the datagrid, I add a colummn call "Image" <code> <asp:datagrid id="dgSearchResult" runat="server"><Columns><asp:TemplateColumn HeaderText="Image" ItemStyle-Width="55px" ItemStyle-HorizontalAlign=Center> <ItemTemplate> <asp:Image ID=ThumbImage Wi...

Cast from type 'DBNull' to type 'String' is not valid
 Hi all,Sorry to be posting here on what is probably a common enough error but I am at my wits end with this one. Quite simply I call an Update stored procedure in my code, passing in a number of paramaters. However, when I try to run the code, the following exception is thrown:Cast from type 'DBNull' to type 'String' is not validI'm really not sure what the problem is here. I have tried hardcoding values for the string values with no success also, as well as using the IsNothing function, but no joy there eitherI would really appreciate someones help as I spent 2 hou...

Cast from type 'DBNull' to type 'String' is not valid #2
G'day, Puzzled by this. The error occurs on the field "Mobile" yet its format is exactly the same as the other fields. In otherwords I can remove the line of code dealing with "Mobile" and it will function. Can anyone explain? The error is : Cast from type 'DBNull' to type 'String' is not valid. <%#ifPresent("Phone:",Container.DataItem("Phone"))%> <%#ifPresent("Fax:",Container.DataItem("Fax"))%> <%#ifPresent("Mobile:",Container.DataItem("Mobile"))%> <%#ifPre...

Cast from type 'DBNull' to type 'String' is not valid. #3
Hi Everyone, I am trying this very simple select statement and storing my information into arrays but i am receiving the above error whenever the field has a NULL as value.I have looked at other similar post in this forum and know that i need to check if the string is empty but somehow i do not know the exact syntax to do this check.  Please help. Dim conAct As SqlConnectionDim cmdSelectAct As SqlCommandDim dtrAct As SqlDataReaderconAct = New SqlConnection("etc etc")conAct.Open()cmdSelectAct = New SqlCommand("Select EMPLID,USERID etc etc  from TABLE where STATUS='I'", conAct)...

Cast from type 'DBNull' to type 'String' is not valid. #2
i have an login page which use e-mail address and password to login , once the system comfirm this is valid login it get customer details and in my login page i have the following code Dim loginsystem As onlineshop.Customers = New onlineshop.Customers() Dim customerId As String = loginsystem.customer_login(login.email_address, login.password) If customerId <> "" Then Dim customerDetails As onlineshop.customer_Detail = loginsystem.Get_Customer_Details(customerid) and at the onlineshop.customer_detail function i have the following ...

Cast from type 'DBNull' to type 'String' is not valid.
I have a problem when I try to read from a table "Ads" in my database and the value of the field is <NULL>. (I assume that I will come across a field that will be blank.) I’m not referencing any field in particular. I am learning .Net through I test project. When I delete a field in a record, for example "phone" I get following error: Cast from type 'DBNull' to type 'String' is not valid. Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid. I would like to not show the label at all if it's null. Fo...

Cast from type 'DBNull' to type 'String' is not valid. #2
I think this is a bug, the database of Products are allowed to have "Null" Value, but when you do have one, the store is crashed with subjected message, I have found a solution to use View to change the Null value into something when run the SQL store procedure, but I don't know if is there anyway from "Class" side to solve it. Always check for Null before trying to convert the value to a string or assign to a text or label. Try this: If Not(IsDbNull(drProducts("foo"))) Then ...insert your code to display here... End If mix it up! www.devalch...

Cast from type 'DBNull' to type 'String' is not valid. #3
help anybody.. it has been 2 weeks since i got out of this error.. please anybody share their insights on this here is my code. Populate Struct using Output Params from SPROC Line 86: myCustomerDetails.FullName = CStr(parameterFullName.Value) ---ERROR!! Line 87: myCustomerDetails.Password = CStr(parameterPassword.Value) Line 88: myCustomerDetails.Email = CStr(parameterEmail.Value) Of course. They are not the same thing. casting requires the objects to implement either a common ancestor or interface to cast to. Use ToStrin...

Cast from type 'DBNull' to type 'String' is not valid. Error
Hi, Can anyone help me with an error I am receiving "Cast from type 'DBNull' to type 'String' is not valid."  I am creating an aspx page in VB.NET, please find attached code causing error.objCmd = New SqlCommand("USP_DisplayEmbarkDetails", objConn)  objCmd.CommandType = CommandType.StoredProcedure  objCmd.Parameters.Add("@BranchID", lblGetBranchID.Text)  objConn.Open()  objRdr = objCmd.ExecuteReader()    While objRdr.Read     lblBranchName.Text=objRdr("BranchName")    lblBranchID.Text=objRd...

"cast from type 'dbnull' to type 'string' is not valid"
I keep getting the following error in my code: "cast from type 'dbnull' to type 'string' is not valid"My code is as follows:Do While DR.Read tbxRefNo.Text = Trim(DR("Ref")) tbxTitle.Text = Trim(DR("Title")) If Not IsDBNull(DR("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If tbxStatus.Text = DR("StatusName") tbxSignedBy.Text = DR("SignedBy") LoopI am reading the values from a database using the loop but I was getting a problem when I read a record that had a NULL value in the "Signed" field. I changed the code to put in the I Not IsDBNull...&nbs...

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 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...

replace Null values
I am starting to understand why the error "Cast from type 'DBNull' to type 'String' is not valid." is happening, but I am not sure how to determine WHICH line is failing ( I am in the middle of a DotNetNuke app and very new to .NET in general )..But I can see the replace_Null function is failing.the function:Private Function replace_Null(ByVal myobj As Object) As StringIf IsDBNull(myobj) ThenReturn ""ElseReturn CStr(myobj)End IfEnd Functionand the section of code that calls the function.Is there a way like in classic asp to Response.Write = "HERE"...

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'.
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...

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. ...

Unable to cast object of type 'TYPE' to type 'TYPE'.
I've got a custom class and collection...I want to use them to databind a Grid, and persist the state using a Session variable.  However I keep getting the above error **Sometimes...There will be times when it works fine, and times when it totally craps out.System.InvalidCastException: Unable to cast object of type 'ApprovalCycleCollection' to type 'ApprovalCycleCollection'. This should work fine, no?  I want a property to use in all my foreach loops   public ApprovalCycleCollection NewApprovalCycle { get { if (Se...

Cast from type 'DBNull' to type 'Integer' is not valid.
I tried string and integer, still does not like the cast.. Any suggestions Dim intPrefixID As Integer = IIf(cboPrefix.SelectedItem.Value = "NONE SELECTED", System.DBNull.Value, cboPrefix.SelectedItem.Value) You can't do that. Instead, if the SelectedItem.Value is NONE SELECTED, set the intPrefixID to null. Then, in your data access code, check to see if it is null. If so, assign the parameter the DBNull.Value. Otherwise, assign it the SelectedItem.Value.Darrell Norton, MVPDarrell Norton's BlogPlease mark this post as answered if it helped you! I'm confuse...

Web resources about - Cast from type 'DBNull' to type 'String' is not valid. - asp.net.object-datasource

Resources last updated: 2/25/2016 5:38:08 AM