Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Simple" syntax question. 3

Status
Not open for further replies.

nickmollberg

Programmer
Aug 2, 2004
29
US
I have a dataset, and am reading values out of it, binding them to controls.

the if statement below worked fine when the column 'custnumber' was a varchar, but when converting it to a smallint, the code below no longer works (as one might imagine.)

if(sdr[ "CustNumber" ] ).Equals( "155" ) )
{
do code...
}

so, if the customer number (as read from the SqlDataReader) is 155, then do the code inside the if statement.

so, I just need the code above, but for an integer value. I've tried tons of different ways, but had no luck.

A silly question, I know, but I'm going nuts over here.
Thank you!
 
try
Code:
if(Int32.Parse(sdr["CustNumber"].ToString()) == 155){

}
Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
You can also write without using Parse which must be enclosed in try-catch block if the CustNumber allows DBNull.
Code:
if((System.Int16)sdr[ "CustNumber" ]=155  )
{
do code...
}
The above code also will throw an exception if that column allows NULL in the source database.
If that is true , you have two choices:
- enclose the if in try-catch block and process the exception
- check the sdr[ "CustNumber" ]. If its type is System.DBNull then do not check it with 155.
-obislavu-
 
are you sure it's not...
Code:
if((System.Int16)sdr[ "CustNumber" ][b]==[/b]155  )
{
do code...
}

;-)

the = operator tries to assign a value
the == operator tests the 2 values

you generally want to use == in an IF statment...

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
if(Int32.Parse(sdr["CustNumber"].ToString()) == 155){}

Worked just fine.

What I really need is some massive source for source code examples. That, and lots more time with a c #book.
 
What I really need is some massive source for source code examples. That, and lots more time with a c #book.
yeah me too...

Google and this forum have been a big help so far...

The String/Integer/Char casting thing has been killing me ;-)

I'm used to VB being able to say i = val(s) or s=str(i)

I'm just not used to these Parse, ToString, and ToCharArray things...

DLL's are driving me crazy now though... but that is about to be another post...

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Sorry there was a typing error but the following is working
Should be == and not =.
Code:
if((System.Int16)sdr[ "CustNumber" ]==155  )
{
do code...
}
This form is more performant than Parse().
-obislavu-
 
obislavu said:
Sorry there was a typing error but the following is working
Should be == and not =.

I do the same thing all the time...

That and forgetting to put () after a function/sub with no parameters...

I've been using VB6 too long ;-)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Friendly tip for those of us who can't count equal signs.

It's a good idea to put left side values on the right side of the equation... as counterintuitive as it seems to the hundreds of programming examples and math proofs we've all done....
if (155 = whatever.The.Example.Was()) {

}

will throw a nice quick parse error, whereas

if (whatever.The.Example.Was() = 155) {

}

will create a bug that's a hassle to track down if you're not looking for it.
 
hmmm... Never thought of that, I'll give ya a star ;-)

(Now I just have to remember to start doing that :))

Too bad you can't just overload = it to work the same way as == if used in a boolean context...

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top