|
-
Feb 23rd, 2007, 07:32 AM
#1
Thread Starter
Lively Member
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:
left(row.item("field"), 25)
but I cant figure out how to do it in C#
-
Feb 23rd, 2007, 12:02 PM
#2
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>
-
Feb 23rd, 2007, 12:03 PM
#3
Thread Starter
Lively Member
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...
-
Feb 23rd, 2007, 12:20 PM
#4
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).
-
Feb 23rd, 2007, 12:27 PM
#5
Thread Starter
Lively Member
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...
-
Feb 23rd, 2007, 02:19 PM
#6
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.
-
Feb 24th, 2007, 09:27 AM
#7
Thread Starter
Lively Member
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.
-
Feb 25th, 2007, 11:03 PM
#8
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|