Asked by:
Displaying datareader content on aspx page

Question
-
User-2006371901 posted
I've done this with helper functions in the past, but i can't recall how I was able to take data from a datareader in codebehind and display it in aspx html page:
(codebehind):
...
string panelcolor
.....
cmd.Connection.Open();
SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();
panelcolor = rdr["bgcolor"].ToString();
(aspx/html page):
<div class="panel-heading" style="background-color:<%= panelcolor%>;">
error : 'panelcolor ' does not exist in current context
??
Thanks in advance
Ned
Monday, September 24, 2018 10:52 PM
All replies
-
User475983607 posted
A DataReader reads data from a SQL connection usually in a loop. The ASP.NET DataReader docs explain the details. IF you are returning a single value use execute scalar.
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-data-using-a-datareader
Other than that there is no indication in the code where, when or how panelcolor is defined or if the variable contains a value after the query execution. Usually, it's a Page class property but there is just not enough information to guess. I would start but setting a breakpoint and single stepping through the code. What is the value of panelcolor? Is there anyway you can post all the relevant code or code that reproduces the issue?
Monday, September 24, 2018 11:06 PM -
User-2006371901 posted
//the stored procedure spGetItems with @Qreg param
select bgcolor, img from colors where region = @Qreg;//code behind
string qreg = Request.QueryString["QReg"];
string panelcolor;SqlCommand cmd = new SqlCommand("spGetItems", new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]));
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Qreg", qreg);cmd.Connection.Open();
SqlDataReader rdr = cmd.ExecuteReader();rdr.Read();
panelcolor = rdr["bgcolor"].ToString();
... i need to call panelcolor from code behind to represent the color of the panel element in the aspx htmlTuesday, September 25, 2018 3:09 PM -
User475983607 posted
There is no way to evaluate the code as written. The is code incomplete with likely bugs. I recommend using the Vistula Studio debugger to place a breakpoint and step through the logic.
For the second time, I recommend using ExecuteScalar when returning a single value form a query. Have you, at least, verified panelcolor contains the expected value?
Norkle
... i need to a way to call panelcolor as some sort of function to represent the color of the panel element in the aspx htmlThe code should work as long as panelcolor has a properly formatted result.
Working example.
namespace WebFormsApp { public partial class _Default : Page { public string PanelColor { get; set; } protected void Page_Load(object sender, EventArgs e) { PanelColor = "pink"; } } }
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebFormsApp._Default" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <div class="row" style="background-color: <%=PanelColor%>;"> <div class="col-md-12"> Hello World </div> </div> </asp:Content>
Tuesday, September 25, 2018 3:32 PM -
User-2006371901 posted
The code works in this premise, what isn't happening is 'panelcolor' is not called correctly as <% panelcolor%> in the aspx page. panelcolor = the bgcolor value of whatever qreg 'region' the user supplies upon coming to the page.
Any other insights welcomed.HTH
NedTuesday, September 25, 2018 4:04 PM -
User475983607 posted
Norkle
The code works in this premise, what isn't happening is 'panelcolor' is not called correctly as <% panelcolor%> in the aspx page. panelcolor = the bgcolor value of whatever qreg 'region' the user supplies upon coming to the page.
Any other insights welcomed.I have no idea what this statement means, "'panelcolor' is not called correctly as <% panelcolor%> in the aspx page".
Form this response, is seems you have bugs elsewhere in the code base. There's no indication in the code above related to "qreq" or "regions" or how the user gets to this page. Plus you have not answered a single troubleshooting question.
Simply, you have not provided enough information to guess where the bugs are located in your code.
Tuesday, September 25, 2018 5:34 PM