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

Hi,

it's me again...

I'm having error with

Dim 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
            Dim reader As SqlDataReader = cmd3.ExecuteReader

            Do While reader.Read()
                afirstname = reader("FirstName")
                alastname = reader("LastName")
              

                Dim writer As IO.TextWriter = New IO.StreamWriter("/temporaryfile.html")
                writer.WriteLine("<html><html>">
                writer.Close()

            Loop
            reader.Close()

It's showing me this

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

How can i solve it.

Thanks

0
Metaferia
8/31/2008 7:35:16 PM
asp.net.sql-datasource 29906 articles. 0 followers. Follow

3 Replies
772 Views

Similar Articles

[PageSpeed] 39

Hi Metaferia,

If a value returned from the database is null, then reader("someField") returns DBNull, which cannot be converted to a ttring (as you've seen).  To get around this, for any field that can be null in teh database, you need to check for DBNull before assigning it to a string.  There are some examples at http://www.shiningstar.net/blog/DataReader.aspx.

Hope that helps.

Aaron


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 so everyone will know you have been helped.
0
agolden
8/31/2008 9:01:39 PM

Metaferia:
afirstname = reader("FirstName")
                alastname = reader("LastName")

if you change the above 2 lines like below,its enough.

 afirstname = reader("FirstName").ToString()
                alastname = reader("LastName").ToString()


Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
0
ramireddyindia
9/1/2008 5:10:11 AM

 Thanks

 

Simple and clean 

0
Metaferia
9/1/2008 12:23:23 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...

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

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
I get an error (Conversion from type 'DBNull' to type 'String' is not valid.) when I try to assign a field value to a text box using the datareader. Is there code that will fix this error?  wrap an IF check around the retrieval and check for DBNull you'll need to check for null first, before you assign it - - Something like: if Not dr("Yourfield") is System.DbNull.Value then...........David WierMCP/ASPInsiderASPNet101.com - where to look first!Please Vote for ASPNet101 - 'Best Community Resource'!Control Grouper - easily control pr...

Conversion from type 'DBNull' to type 'String' is not valid
I get this error when I try to add an event:Conversion from type 'DBNull' to type 'String' is not validI've been able to add events before, but all of the sudden I'm getting the error...Here is the output of the error:Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.Source Error: Line 115: </h3> Line 116: <p> Line 117: <asp:Label ID="descriptionLabel" runat="server" Text='<%# truncate(CStr(Eval("description"))) %>' />...

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

I get Conversion from type 'DBNull' to type 'String' is not valid.
I get the following errors when I try and access certain pages.  It is caused by not populating all the text boxes with data and subsequent viewing causes these conversion errors.  Does any one else experience this?  How have you guys worked around this problem? 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: Conversion f...

Conversion from type 'DBNull' to type 'String' is not valid #2
I'm getting a error message saying: Server Error in '/' Application. 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: Conversion from type 'DBNull' to type 'String' is not valid.Source Error: Line 113: </h3> Line 114: ...

Conversion from type 'DBNull' to type 'String' is not valid #2
i have with my DB fetch content is Ogject class so i am not use any dataset or datatable but i have problem when field in DB is empty i can insert DBNull in string like this curr.visible = r("visible") curr.title = r("title")curr.description = r("description") curr.price = r("price")   how can i  solve this problem without put if with every insert code fedailr:curr.visible = r("visible") curr.title = r("title")curr.description = r("description") curr.price = r("price") So I assume r ...

Bound TextBox error: Conversion from type 'DBNull' to type 'String' is not valid
 Hi,I use DataSet designer to generate my strongly typed datasets and to bind my FormView to a Business Layer (BLL). The BLL uses the generated Dataset classes. I have a column  in the DB of type string. The column is bound to a textbox like this:Text='<%# Bind("Local") %>'  If the column has a value, thingts work well, but  When the column has a null value, I get the error: Conversion from type 'DBNull' to type 'String' is not validWhat can I do Please? Thanks a lot, I appreciate your taking the time to help me. ...

Conversion from type 'DBNull' to type 'Boolean' is not valid.
Hi all I'm creating a multiple view form using FormViews within a Wizard control. I've been working my way through various problems and I'm getting close but get the following error when I try to Update. System.InvalidCastException was unhandled by user code  Message="Conversion from type 'DBNull' to type 'Boolean' is not valid."  Source="Microsoft.VisualBasic"  StackTrace:       at Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(Object Value)        at ASP.Referral_aspx.__DataBinding__control287(Obj...

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

Resources last updated: 11/25/2015 1:00:25 PM