Results 1 to 8 of 8

Thread: specific number of characters ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    Denmark
    Posts
    67

    specific number of characters ?

    I want to pull a specific number of characters out from my database...

    I think this is the VB version of it...
    VB Code:
    1. left(row.item("field"), 25)

    but I cant figure out how to do it in C#

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: specific number of characters ?

    You didn't specify which DB server you're using...
    SQL Server: LEFT function
    Access: (guessing here...) LEFT
    Oracle: INSTR[B]/SUBSTR[B] <http://www.psoug.org/reference/substr_instr.html>
    MySQL: LEFT <http://dev.mysql.com/doc/refman/5.0/en/string-functions.html>

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    Denmark
    Posts
    67

    Re: specific number of characters ?

    im using access database, but how do is it suposed to look in C# because I cant use the "left" in C# :S it doesnt show...

  4. #4
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: specific number of characters ?

    Ah, missed the "how to in C#" line there...

    You need to get a string instance from the appropriate field (e.g. <IDataReader instance>.GetString(<field index>)), and then use <string instance>.Substring(0, <number of characters).

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    Denmark
    Posts
    67

    Re: specific number of characters ?

    this is my code...

    Code:
                OleDbConnection con = new OleDbConnection("Provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("App_Data/fliksdata.mdb"));
                OleDbDataAdapter adp = new OleDbDataAdapter("select * from tblnews where fldid=" + Request.QueryString["newsid"], con);
                DataSet dst = new DataSet();
    
                adp.Fill(dst);
                foreach (DataRow row in dst.Tables[0].Rows)
                {
                    lbloutput.Text = "<table cellpadding='0' cellspacing='0' border='0'><tr><td width='440'><font color='#296789'><b>" + row["fldoverskrift"] + "</b></font></td></tr><tr><td width='440'><font color='#296789'>" + row["fldtekst"] + "</font></td></tr><tr><td width='440'><font color='#296789'><br/><a href='javascript:history.back()'><b> << Tilbage</b></a></font></td></tr></table><br/><br/>";
                }
    this line
    Code:
    <font color='#296789'>" + row["fldtekst"] + "</font>
    I want to only get 25 characters in the row["fldtekst"] field...

  6. #6
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: specific number of characters ?

    Code:
    <font color='#296789'>" + row["fldtekst"].Substring(0, 25) + "</font>
    You may need to either ToString() or Convert.ToString() if row["fldtekst"] is not a string.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Location
    Denmark
    Posts
    67

    Re: specific number of characters ?

    Its working now thank you

    Here is the working code...
    Code:
                OleDbConnection con3 = new OleDbConnection("Provider=microsoft.jet.oledb.4.0;data source=" + Server.MapPath("App_Data/fliksdata.mdb"));
                OleDbDataAdapter adp3 = new OleDbDataAdapter("select * from tblnews order by fldid desc", con3);
                DataSet dst3 = new DataSet();
    
                adp3.Fill(dst3);
                foreach (DataRow row in dst3.Tables[0].Rows)
                {
                    lblnews.Text += "<table cellpadding='0' cellspacing='0' border='0'><tr><td width='140'><font color='#296789'><b>" + row["fldoverskrift"] + "</b></font></td></tr><tr><td width='140'><font color='#296789'>" + row["fldtekst"].ToString().Substring(0, 10) + "</font></td></tr><tr><td width='140'><font color='#296789'><a href='news.aspx?newsid=" + row["fldid"] + "'><b>Læs Mere</b></a></font></td></tr></table><br/><br/>";
                }
    Last edited by RoflJOE; Feb 24th, 2007 at 09:30 AM.

  8. #8
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: specific number of characters ?

    you can set during select
    ex
    select substring(fieldname,1,25) fieldname from tablename

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width