'DBNull' to type 'String' is not valid

Can anyone help me solve this error?  I think I am getting this because there is a NULL value in the database for the field.  Here is my code:

While objRdr.Read()

txtPhone2.Text=objRdr("SecondaryPhone")

 I also tried:

While objRdr.Read()

txtPhone2.Text=CStr(objRdr("SecondaryPhone"))

I am not using a repeater or datalist because I am only pulling a single record for the purpose of editing it.

I am still relatively new, and woulld appreciate as much detail as you can provide.  I am using SQL Server and VB.net.

0
Nashua
9/2/2008 1:18:16 AM
asp.net.web-forms 93655 articles. 5 followers. Follow

8 Replies
473 Views

Similar Articles

[PageSpeed] 10

Hi There,

try following:

 While objReader.Read()

If Not objReader.IsDBNull(2) Then

txtPhone2.Text = objReader.GetString(2)

End If

End While

Also worth reading following:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getstring.aspx

Hope it helps! 

 


DC517
Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved.
0
d4dennis
9/2/2008 1:34:33 AM

Hi,

try this

While objRdr.Read()
  If Not objRdr("SecondaryPhone") Is DBNull.Value Then
      txtPhone2.Text=objRdr("SecondaryPhone")
  end if
End While

 

 

Please don't forget to mark it as an answer.


Don't forget to tag me as an answer if it helps you.


-------------------------------------------------


Everyone lives in one of two tents: Con-TENT or discon-TENT
0
cliquerz
9/2/2008 1:39:47 AM

 You  actually have a NULL value on your DB, so when you try to parse that field you get that error, I don't recommend leaving "NULL" values on the DB I do prefer leaving a white space or a zero.

 =)


It is not enough to do your best: you must know what to do, and THEN do your best.

-- W. Edwards Deming
0
gsosamv
9/2/2008 2:04:18 AM

What do I do if it is NULL?  Do I just ignore it?

0
Nashua
9/2/2008 1:21:34 PM

I see that I have the "NULL" value there when I go directly into the database.  How would I go about following your idea of leaving a white space there instead of a NULL value?

0
Nashua
9/2/2008 1:23:00 PM

you can do that at insertion time you can save ' ' value or u can edit by ' ' also

but better u should use if not isdbnull


HAPPY CODING
http://shobhitrai.blogspot.com/
0
shobhit
9/2/2008 1:27:25 PM

What is the 2 in IsDBNull(2) and GetString(2) ?

0
Nashua
9/2/2008 1:28:29 PM

2 is the second column in your data table u can use column name also


HAPPY CODING
http://shobhitrai.blogspot.com/
0
shobhit
9/2/2008 1:29:50 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...

''''''''''''''''''''
Name: haznen Email: haznenatyahoodotcom Product: Gran Paradiso Alpha 8 Summary: '''''''''''''''''''' Comments: '''''''''''''''''''''''''''''''''''' Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9a1) Gecko/20061204 UGES/1.7.2.0 GranParadiso/3.0a1 From URL: http://www.mozilla.org/projects/granparadiso/ Note to readers: Hendrix gives...

Cast from type 'DBNull' to type 'String' is not valid.
On the viewproduct page if any of the db fields referenced in the SP are empty you get the above error, the error originates in the productsDB.vb file as follows myProductDetails.ModelNumber = CStr(parameterModelNumber.Value) myProductDetails.ModelName = CStr(parameterModelName.Value) myProductDetails.ProductImage = CStr(parameterProductImage.Value).Trim() myProductDetails.UnitCost = CType(parameterUnitCost.Value, Decimal) myProductDetails.Description = CStr(parameterDescription.Value).Trim() OK I can make ...

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

Conversion from type 'DBNull' to type 'String' is not valid
Hello Friends            How are you?? Friends i am getting problem in SQL Server 2005. I am deployng web application on production server as well as Databse also. In production server i inserted new field in all tables which is rowguid and its type is uniqueidentifier. The default binding for this field is newsequentialid(). In some pages it works ok but in some places it generates error like 'Conversion from type 'DBNull' to type 'String' is not valid'. Can anybody help me to solve this problem. Its urgent so plz re...

Conversion from type 'DBNull' to type 'String' is not valid.
 Hello Experts,I am reading data into a datagrid and am checking for nullsHere is the code in the datagrid which checks a function in the code-behind:<ItemTemplate>    <asp:Label id="Label2" runat="server" text='<%# GetPickUpDate(container.dataitem("ActualPickUpDate"))%>'></asp:Label></ItemTemplate> Here is the funtion:Function GetPickUpDate(ByVal DateVal As Date) As String            If Not IsDBNull(DateVal) Then     &nb...

Conversion from type 'DBNull' to type 'String' is not valid.
Hi,it's me again...I'm having error withDim Accesstring As String = ("SELECT     FirstName, LastName, Email, StreetAddress, City, Country, State, Zip, Worknum, googlecheckout, paypal, csftcc, confirmationcode FROM dbo.reservation WHERE (confirmationcode = @concode)")            Dim cmd3 As New SqlCommand(Accesstring, icnnn)            cmd3.Parameters.Add("@concode", SqlDbType.NChar).Value = onlineconfirmationcode.Text   &...

Conversion from type 'DBNull' to type 'String' is not valid...
Thanks for your time.  Getting following error when grid source populates a rows Date field with Null:     - Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.It works works when I filter the nulls out, but I need to show all rows in the grid. Boils down to:  a) How could I maintain a blank value in the grid and cast this properly to include all rows?    < or >  b) Is there a way to default a value that isnt a date (to avoid the null & keep the IsDate logic as is)  c) ...

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.
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. Change your line to _Address2=dtrUserDetails("D_Address2") & "" The empty string in the end will convert the filed to a string ...

Conversion from type 'DBNull' to type 'String' is not valid
Hello! All,I try to use ObjectDataSource with business layers and used the code given by SDK 2.0. But I got the following message: Conversion from type 'DBNull' to type 'String' is not valid.  I couldn't figure out what 's the problem and felt sorry about this.I attached my code below.  Can anyone help me out of it. Thanks a lot! Vivia  Error Massage: 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 ...

Conversion from type 'DBNull' to type 'String' is not valid
I am using Jeremy's Personal Web Site, which I must say is great.  I have uploaded the site to my GoDaddy web hosting account and created the database tables.  I created an entry for a user and for the site_settings.  I am getting the following error: Conversion from type 'DBNull' to type 'String' is not valid. 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: C...

Cast from type 'DBNull' to type 'String' is not valid.
Hi, I have a page with several textboxes...it's a "change your account" page. One of the parameters is presented as a label instead since it is not changed ON THIS PAGE. It needs to be NULL when the account is created and entered later. But when the page loads, I get the above error. The account row is loaded from a business object LoadFromId(). How can I load the page even though this label data is NULL to start with? relevent code below. <asp:Label ID="lblDDAccount" Runat="server"></asp:Label>------If currentUser.DDAccount = "" Then       &...

Web resources about - 'DBNull' to type 'String' is not valid - asp.net.web-forms

Resources last updated: 11/28/2015 11:11:48 AM