I’m trying to read items from a DataTable into a webpage, but getting 2 errors in the C# code: p1.InnerHTML = dt.rows(0).Item(0);
Errors:
p1 does not exist in the current content.
System.Data.Datatable does not contain a definition for ‘rows’ and no extension method ‘rows’ accepting a first argument ..
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
string strConnection = ConfigurationManager.ConnectionStrings["ChinatowndbConnString"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnection);
string sql = "SELECT pagetext FROM Content where pagetag = 1";
SqlCommand cmd = new SqlCommand(sql);
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
SqlDataAdapter sd = new SqlDataAdapter(cmd);
sd.Fill(dt);
p1.InnerHTML = dt.rows(0).Item(0);
p2.InnerHTML = dt.rows(1).Item(0);
p3.InnerHTML = dt.rows(2).Item(0);
}
HTML:
<p id="p1" class="none"></p>
<p id="p2" class="none"></p>
<p id="p3" class="none"></p>
Any ideas how to get my code working,
Thanks
Tea
pis a html element, you must addrunat=serverif you want to access it on serversideC# is case-sensitive and the property is called
Rowsinstead ofrowsFurthermore, in C# an arry indexer is accessed via square brackets instead of parentheses. But i would suggest to use the
DataRow.Fieldextension method instead:Final note: you have also mispelled
InnerHtml. Visual Studio can correct it automatically for you if the cursor is somewhere inInnerHTMLand you press Ctrl+Space.