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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitting and determining 1

Status
Not open for further replies.

copeZero

Programmer
Aug 31, 2007
46
CA
Hi i'm trying to split a return db value. I've tried reworking the code but nothing seems to work. Maybe a more refreshed brain can assist? Below is an example of what i've tried, the returned value format: bit|char (eg. "0|John Smith"

i need to determine if the bit value returned is zero or one. Thoughts? Thanks.

error: cannot convert type string to bool

Code:
...
object isAlready = cmd.ExecuteScalar();
con.Close();

string[] arrVals = isAlready.ToString().Split(new Char[] { '|' }, 2);

if (arrVals[0] = "0")
{
...
 
that's convoluted.

You have a string "0|John Smith"

If you want to split it you can do that easily
string dbstr = "0|John Smith";
string[] items = dbstr.Split("'");

bool bitval = Convert.ToBoolean(items[0]);
string name = items[1];

if (bitval)
{
//then the value is 1
}
else
{
//The value is 0
}

 
Thanks JurkMonkey,

i'm having problems trying to understand the simple code,

you are setting bitval to either be tru or false based on the first item in the array (which can be a 0 or a 1)

then in the if statement you state:
if (bitval) {
does that state that if bitval is true then...

so if i'm "on the ball" so far, could i also state this:

if (!bitval) {..} else {..}

which would mean: if bitval is false then ...
else ...

Thanks
 
Thanks i got it working and tried the flipSide of the bitval in the if statement as i asked in the last post, all if fine. Thanks!

1 more question if you wouldn't mind...

i would like to use the same code and add to it but i can't seem to find a way, this is what i have:

if (bitval)
{
string Jscript = "<script language=javascript>alert('hi there');</script>";
ClientScriptManager.RegisterStartupScript(this.GetType(), "FOCUS", Jscript);


}
else
{ ...

// i'm trying to create an alert prompt, any suggestions? THnaks again.
 
ahh...

Page.ClientScript.RegisterClientScriptBlock
 
That was the easiest star of my life. :)

As for creating the script - I'm not an asp.net guy, however my new job is going to force me that way.

I do know that in PHP you can simply write to the file directly as it's loading, then that script is available for use from within the application.

In terms of good coding practice, you generally create your javascript methods in a separate file and just include them in your document.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top