Conversion from String to other types

I have a form where  I located 2 text boxes: one to take the budget (decimal data type) and other to take the date (date data type) and I wrote my insert statements after saveing the values of the textboxes into variables by this way and it worked sucessfully when I press the submit button

pBudget = pbudgetTxt.Text

toDate = odateTxt.Text

 

However, when I keep the text boxes empty and press submit. It gives me the following error

Converting from string "" to type Decimal/Date/whatever... is not valid!!!

 I tried to use the convert method, but this still causing some other errors .. Please help

pBudget = Convert.ToDecimal(pbudgetTxt.Text)

toDate = Convert.ToDateTime(odateTxt.Text)

0
budoor
5/4/2009 8:55:51 AM
asp.net.sql-datasource 29906 articles. 0 followers. Follow

4 Replies
629 Views

Similar Articles

[PageSpeed] 18

This is due to when the textbox is empty it converts null string "" to decimal which is an error in .net to overcome this problem
Try This
If len(trim(pbudgetTxt.Text))>0 then
pBudget = Convert.ToDecimal(pbudgetTxt.Text)
End If


Regards
ASIF
Please remember to click “Mark as Answer” on the post that helps you.
This can be beneficial to other community members reading the thread.
0
asifchouhan
5/4/2009 10:03:52 AM

 OR SOMETHING LIKE THIS:

            if (pbudgetTxt.Text.Length  > 0)
            {
                pBudget =  Convert.ToDecimal(pbudgetTxt.Text);
            }

            if (odateTxt.Text.Length > 0 )
            {
                toDate = Convert.ToDateTime(odateTxt.Text);
            }

Regards,

Yoga

0
tgyoga
5/4/2009 10:11:10 AM

does your program allow budget = null? if not pls put a validator to make sure the user key in the value - easiet way

or

if user didn't key in anything, does the system expect the budget to be 0? if so, write a function and pass in the pbudgetTxt.Text e.g

Dim dc As Decimal = 0.0

if pBudgetText.Text = "" Then

dc = Convert.ToDecimal(pBudgetText.Text)

Endif

return dc

0
mo
5/4/2009 2:59:12 PM
Thank you all ... it is the first time I could read a post and based on it solve a problem :) :) :) 
0
budoor
5/5/2009 4:16:48 AM
Reply:

Similar Artilces:

Conversion from type DBNull to type String is not valid
I know which column is DBNULL.  What do I do? I tried using a test in line 20 but this isn't helping.  What does it want?  1 Protected Sub EmailWebmaster_Click(ByVal sender As Object, ByVal e As System.EventArgs) 2 'UPDATE YOUR DATABASE HERE 3 'First this has to send an email and then update the database table for lastemailed date. 4 Dim email As String 5 Dim ID As Integer = DirectCast(sender, Button).CommandArgument 6 Dim tblLinkAdapter As New backlinkTableAdapters.tblLinkTableAdapter 7 Di...

Conversion from type 'DBNull' to type 'String' is not valid.
On line 65 of Events_Calendar.aspx- <div style="padding: 3px;"> <asp:HyperLink ID="HyperLink1" runat="server" Text='<%#Eval("title") %>' NavigateUrl='<%# "Events_view.aspx?Eventid=" &CStr( Eval("ID"))%>' ToolTip='<%# truncate(CStr(Eval("description"))) %>' /></div> The debugger tells me "InvalidCastException unhandled by user code" I found out that the data entry form for events allows the user to leave the description field empty.  So this translates to NULL in the SQL Server 2005 database.  the NULL value in the description field...

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 'Calendar' to type 'String' is not valid.
I am trying to use the asp:calendar to update data into my db with the gridview, I can get the gridview to display the date that is currently in the DB on the calendar, I can also select a new date that i want to change it to. However when the update gets run I get the below error. How can I correct this? Also is there a way that I can have the calendar display as a dropdownbox, rather than having it show the entire month? Error is below, below that i my code.Conversion from type 'Calendar' to type 'String' is not valid. Exception Details: System.InvalidCastException: Conversion from type ...

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

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...
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! 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.
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 #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: ...

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

Web resources about - Conversion from String to other types - asp.net.sql-datasource

Conversion (law) - Wikipedia, the free encyclopedia
Its criminal counterpart is theft . Examples are seen in cases where trees are cut down and the lumber hauled from the land by someone not having ...

Sonny Bill Williams' transformation due to conversion to Islam, Anthony Mundine says
He went from being savaged for walking out on the Bulldogs to becoming one of the world's most respected rugby union stars. Anthony Mundine believes ...

Revisiting Carlos Martinez's rotation conversion
... thanks to his pitch mix, then it sure sounded like it could be possible. Now, almost a full year later, I wanted to revisit Martinez's conversion. ...

Tory MP: 'It is time to make gay conversion therapy illegal'
Gay-conversion therapies were debated in the house of parliament this afternoon with one Conservative MP calling for legislation to make them ...

Roth IRA conversion is complicated
Tax rules governing IRAs are complex and ever changing, so if you're considering a Roth conversion, do some legwork first.

Russian Minister Confirms Plans to Ban Bitcoin-to-Fiat Conversions
Russia's Deputy Finance Minister has confirmed the country's plans to penalise the conversion of cryptocurrencies such as bitcoin into the ruble. ...

Get a free copy of powerful DVD ripping and video conversion software
DVD players are going the way of the Walkman, leaving many of us with vast libraries of obsolete discs. Of course the movies and TV shows themselves ...

This Texas Tech Two-Point Conversion Is A Work Of Art
Every college football play should start like this. Read more...

Advocates back Ohio bill to ban conversion therapy on gay, transgender minors
As a registered nurse and state-licensed social worker, Jody Davis spends much of her time helping others. But as a teen, she was put into conversion ...

5 Ways to Analyze the Path to Conversion
There are many different ways to analyze how a visitor comes to the point of conversion on your website. Over the past month I've been figuring ...

Resources last updated: 12/6/2015 1:25:16 PM