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#
Printable View
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#
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>
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...
Ah, missed the "how to in C#" line there... :o
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).
this is my code...
this lineCode: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/>";
}
I want to only get 25 characters in the row["fldtekst"] field...Code:<font color='#296789'>" + row["fldtekst"] + "</font>
You may need to either ToString() or Convert.ToString() if row["fldtekst"] is not a string.Code:<font color='#296789'>" + row["fldtekst"].Substring(0, 25) + "</font>
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/>";
}
you can set during select
ex
select substring(fieldname,1,25) fieldname from tablename