How can I split database results with ExecuteReader and Split?

Discussion in 'ASP .Net' started by needin4mation@gmail.com, May 4, 2006.

  1. Guest

    Hi, thanks for any help here:

    SqlCommand cmd = new SqlCommand("SELECT categories FROM catalog"
    conn);
    rdr = cmd.ExecuteReader();
    String temp;
    while (rdr.Read())
    {
    temp = rdr[0];
    Response.Write(temp.Split(":"));
    }

    Doesn't work. I get can't convert object to string, and I'm not real
    sure I right here anyway. I think I need another function.

    I have a category column in my database that looks like this:

    column1
    ------------
    Accessories:Women's Socks:Women's Socks - Athletic

    It is a colon delimited list in the column. Now I have to split it
    back up. Can anyone tell me how to split that column when I retrieve
    that row? Do I send:

    Accessories:Women's Socks:Women's Socks - Athletic

    to another function where it iterates through each value (to do
    whatever it is I need done)? Thanks for any help.
     
    , May 4, 2006
    #1
    1. Advertising

  2. You either have to cast the object to string, or use the GetString
    method to get the value as a string:

    temp = rdr.GetString(0);

    Generally it's a bad idea to store several values in a single field in
    the database, though. Which ever way you use the values, you always have
    to go through the process of breaking the string up before you can use
    the values. Actually using the values in a query is near impossible to do.

    wrote:
    > Hi, thanks for any help here:
    >
    > SqlCommand cmd = new SqlCommand("SELECT categories FROM catalog"
    > conn);
    > rdr = cmd.ExecuteReader();
    > String temp;
    > while (rdr.Read())
    > {
    > temp = rdr[0];
    > Response.Write(temp.Split(":"));
    > }
    >
    > Doesn't work. I get can't convert object to string, and I'm not real
    > sure I right here anyway. I think I need another function.
    >
    > I have a category column in my database that looks like this:
    >
    > column1
    > ------------
    > Accessories:Women's Socks:Women's Socks - Athletic
    >
    > It is a colon delimited list in the column. Now I have to split it
    > back up. Can anyone tell me how to split that column when I retrieve
    > that row? Do I send:
    >
    > Accessories:Women's Socks:Women's Socks - Athletic
    >
    > to another function where it iterates through each value (to do
    > whatever it is I need done)? Thanks for any help.
    >
     
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, May 4, 2006
    #2
    1. Advertising

  3. Guest

    I inherited it. I have no choice, but to split it up.

    Thanks for your help.

    Göran Andersson wrote:
    > You either have to cast the object to string, or use the GetString
    > method to get the value as a string:
    >
    > temp = rdr.GetString(0);
    >
    > Generally it's a bad idea to store several values in a single field in
    > the database, though. Which ever way you use the values, you always have
    > to go through the process of breaking the string up before you can use
    > the values. Actually using the values in a query is near impossible to do.
    >
    > wrote:
    > > Hi, thanks for any help here:
    > >
    > > SqlCommand cmd = new SqlCommand("SELECT categories FROM catalog"
    > > conn);
    > > rdr = cmd.ExecuteReader();
    > > String temp;
    > > while (rdr.Read())
    > > {
    > > temp = rdr[0];
    > > Response.Write(temp.Split(":"));
    > > }
    > >
    > > Doesn't work. I get can't convert object to string, and I'm not real
    > > sure I right here anyway. I think I need another function.
    > >
    > > I have a category column in my database that looks like this:
    > >
    > > column1
    > > ------------
    > > Accessories:Women's Socks:Women's Socks - Athletic
    > >
    > > It is a colon delimited list in the column. Now I have to split it
    > > back up. Can anyone tell me how to split that column when I retrieve
    > > that row? Do I send:
    > >
    > > Accessories:Women's Socks:Women's Socks - Athletic
    > >
    > > to another function where it iterates through each value (to do
    > > whatever it is I need done)? Thanks for any help.
    > >
     
    , May 5, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Julian Bowes
    Replies:
    0
    Views:
    723
    Julian Bowes
    Jul 1, 2003
  2. karim
    Replies:
    6
    Views:
    7,892
    karim
    Aug 14, 2003
  3. JD Drewes

    Odd ExecuteReader Error

    JD Drewes, Oct 28, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    408
    JD Drewes
    Oct 28, 2003
  4. MattB
    Replies:
    3
    Views:
    54,792
    nada111
    Oct 4, 2007
  5. =?Utf-8?B?TWlrZQ==?=
    Replies:
    6
    Views:
    5,332
    =?Utf-8?B?TWlrZQ==?=
    Sep 12, 2006
Loading...

Share This Page