473,385 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is a cast to string always possible without exception?

I wonder when an exception raises when I do this:

string str= (string)ds.Tables[0].Rows[0]["CODE"]

The row is always filled with data

If there is a dbnull.value in it, does it raises an error?
And for other data?

thx

Apr 28 '06 #1
5 3240
geronimi wrote:
I wonder when an exception raises when I do this:

string str= (string)ds.Tables[0].Rows[0]["CODE"]

The row is always filled with data

If there is a dbnull.value in it, does it raises an error?
Yes.
And for other data?


If the value isn't a string, you'll get an exception.

Jon

Apr 28 '06 #2
So an integer or object will fail? I thought it will make from a 7 a
"7"

If I do string str=Convert.ToString(ds.Tables[0].Rows[0]["CODE"] )
in stead of string str= (string)ds.Tables[0].Rows[0]["CODE"]

does that make any difference?

Apr 28 '06 #3
geronimi wrote:
So an integer or object will fail? I thought it will make from a 7 a
"7"
Not with a cast, no.
If I do string str=Convert.ToString(ds.Tables[0].Rows[0]["CODE"] )
in stead of string str= (string)ds.Tables[0].Rows[0]["CODE"]

does that make any difference?


Yes, that should be fine. Or just call ToString().

Jon

Apr 28 '06 #4
Hi,
"geronimi" <we******@hotmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
I wonder when an exception raises when I do this:
Whenever the content if not a string or a type that define an explicit
conversion. They are not that much though.

string str= (string)ds.Tables[0].Rows[0]["CODE"]

The row is always filled with data
What kind of data?
If there is a dbnull.value in it, does it raises an error?
YES
try this:
string s = (string) DBNull.Value;
And for other data?


See the first answer, the safest way is first to test for null and assign if
not.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Apr 28 '06 #5
geronimi,

No, that will throw exception if the object is not a string.

You can always use ToString() thought. It will never throw an exception.
--
HTH
Stoitcho Goutsev (100)

"geronimi" <we******@hotmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
I wonder when an exception raises when I do this:

string str= (string)ds.Tables[0].Rows[0]["CODE"]

The row is always filled with data

If there is a dbnull.value in it, does it raises an error?
And for other data?

thx

Apr 28 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Giulio Santorini | last post by:
Hi, I've got a little problem with my C# WinForms test application. I would like to have a ComboBox showing a a visual value and some hidden values. So I build a class able to store thoose...
13
by: Jack MacRank | last post by:
Hello, I'm coding a webform application in C# (ASP.NET 1.1 SP1 with VS.NET 2003 Pro on WinXP SP2 using IIS 5.1). I created a seperate "data" class to house all the MySQL connection and sql...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.