PDA

View Full Version : Retrieve Char value from DB


simpleMel
05-31-2009, 07:22 AM
Anybody has any sample to retrieve from checkboxlist from database that has char value " , " and single value ?

This doesnt work:

if (PortItem.Users.ToString() != null)
{

char[] spl = new char[] { ',' };
//lb_Users = dsPort.Tables["Ports"].Rows[0]["Users"].ToString();

string var = dsPort.Tables["Ports"].Rows[0]["Users"].ToString();
string[] arr = var.ToString().Split(spl);

for (int i = 0; i < arr.Length; i++)
{

if (arr[i].ToString() != "")
{

ch_Users2.Items.FindByText(arr[i].ToString()).Selected = true;

}

}

}

Old Pedant
05-31-2009, 09:31 AM
Ummmm...is this ASP.NET? C#??

YOu are in the wrong forum.

In any case *WHAT* doesn't work??? What part of that code??

Are you saying that the Split does not return an array when there is no comma to split on??

I don't suppose you want to hear that one of the worst database sins you can have is a delimited list stored in a single db field?

Anyway, if the split doesn't produce a one element array, then why not just have an "if" condition that then does the FindByText on the entire field contents??

Oh...and next time, post in the ASP.NET forum, please? And don't just say something "doesn't work"....say WHAT doesn't work.

p.s.: Why do you use ToString() twice on the same value??

simpleMel
06-01-2009, 03:02 AM
Hi Old Pedant,

Thanks for pointing this out, shall create it in ASP.NET next time :)

This line
if (PortItem.Users.ToString() != null) is not set to reference.

Old Pedant
06-01-2009, 11:06 PM
if (PortItem.Users.ToString() != null) is not set to reference.
Huh?

You mean that PortItem itself is null? Or that PortItem.Users is null??

In your first post, you said the problem occurred when the data in the DB did not have a comma. Now you seem to be saying something else entirely. What is the relationship between PortItem and dsPort.Tables["Ports"].Rows[0]["Users"] ???

If any???

simpleMel
06-02-2009, 04:21 AM
Not sure if this work:
string[] arr = lb_Users.Text.ToString().Split(spl);
is not set to reference.



public void gv_OnRowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowIndex == gv.EditIndex)
{
char[] spl ={ ',' };
ds = new DataSet();
ds = otmcl.GetUsers();
dv = new DataView();
dv = dsPortUser.Tables["UsersItem"].DefaultView;

lb_Users = (Label)e.Row.FindControl("lblUsers");

string[] arr = lb_Users.Text.ToString().Split(spl);

//fill up check box
ch_Users2 = (CheckBoxList)e.Row.FindControl("Users");
ch_Users2.DataSource = dv;
ch_Users2.DataTextField = dv.Table.Columns["Users"].ToString();
ch_Users2.DataValueField = dv.Table.Columns["Users"].ToString();
ch_Users2.DataBind();

for (int i = 0; i < arr.Length; i++)
{
if (arr[i].ToString() != "")
{
ch_Users2.Items.FindByText(arr[i].ToString()).Selected = true;
}
}

}
}

Old Pedant
06-02-2009, 05:47 AM
That's strange. I would have thought that Split would work even if there wasn't any match on the splitting character. It would return a one-element array.

The builtin Split function in VB/VBScript/VB.Net works that way.

That is, if you do
Dim foo As String() = Split("xyz", "#")
then foo is a one element array with "xyz" in its only element.

But I guess the Split method doesn't work if there is nothing to Split.

Okay, so as I said, there's an easy work-around:

string[] arr;
string txt = lb_Users.Text.ToString();
if ( txt.indexOf( "," ) < 0 )
{
arr = new string[1];
arr[0] = txt
} else {
arr = lb_Users.Text.ToString().Split(spl);
}
... and then continue ...


See? If the comma does not exist in the text, then craft a one element array and put the entire text into that element. Else go ahead with the split.

simpleMel
06-02-2009, 08:13 AM
Was trying it out but C# do not allow the use of "indexof" unless i write a javascript.

Old Pedant
06-02-2009, 08:48 AM
Oh, just check the docs:
http://msdn.microsoft.com/en-us/library/system.string.aspx

http://msdn.microsoft.com/en-us/library/system.string_members.aspx

It's spelled "IndexOf" instead of "indexOf". Trust MS to do everything differently, making methods always start with upper case letter.

When in doubt, RTFM.

And taking my own advice, I looked here:
http://msdn.microsoft.com/en-us/library/b873y76a.aspx

The docs for Split. And they say, in the Remarks:
If this instance does not contain any of the characters in separator, the returned array consists of a single element that contains this instance.

So... So the Split *should* have worked, even when the field did NOT contain a comma. Which says to me that maybe the field is empty/null instead of just missing a comma. (And, incidentally, means that my IndexOf code should NOT be needed.)

Did you DEBUG??? Did you put breakpoints in the code and check what the values of the various variables are??

simpleMel
06-02-2009, 09:09 AM
Thanks.. ok i tried and i got the checkboxlist to show without selection
but when i select the data with User1 , User2
Response.Write is able to show User1 , User2,
but said that array is out of bounds when only a single value selected.


if (dsPort.Tables["Ports"].Rows[0]["Users"].ToString() != "")
{
char[] spl = new char[] { ',' };
string var = dsPort.Tables["Ports"].Rows[0]["Users"].ToString();
//string[] arr = var.ToString().Split(spl);
string[] arr;

if (var.IndexOf(",") <= 0)
{
arr = new string[0];
arr[2] = var;
Response.Write(var);
}
else
{
var.ToString().Split(spl);

}
}

simpleMel
06-02-2009, 10:17 AM
i can get the value from DB now but not able to select the split checkboxlist :


if (dsPort.Tables["Ports"].Rows[0]["Users"].ToString() != "")
{
string var = dsPort.Tables["Ports"].Rows[0]["Users"].ToString();
string[] spl = var.Split(new Char[] { ',' });
foreach (string s in spl)
{
if (var != "")
{
Response.Write(var);
ch_Users2.Items.FindByText(var.ToString()).Selected = true;
}
var = "";
}
}

simpleMel
06-02-2009, 11:44 AM
Thanks Pedant!

For anybody who might need it


foreach (string s in spl)
{
if (var != "")
{
if (var.ToString() == "val1,val2")
{
ch_Users2.Items[0].Selected = true;
ch_Users2.Items[1].Selected = true;
}
else
{
ch_Users2.Items.FindByText(var.ToString()).Selected = true;
}

}
var = "";
}