Print Page | Close Window

Heading Above The DataGrid

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=16147
Printed Date: 28 March 2026 at 5:59pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Heading Above The DataGrid
Posted By: Misty
Subject: Heading Above The DataGrid
Date Posted: 08 August 2005 at 1:18pm

I'd like to retrieve the field ("Month_Year") from the database below so I can use it as a heading above the datagrid. Please look at the bolded parts of my code below.

Here's my code:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page Language="C#" Debug="true" %>


 <script language=C# runat=server>

string strConnect;
SqlConnection conPayment;
SqlCommand cmdSql;
string strSql;
string strMonthID; 
string MonthName;

void Page_Load()
{
 strConnect = ConfigurationSettings.AppSettings["ConnectionString"];
 conPayment = new SqlConnection(strConnect);
 if (!(IsPostBack)) {
    BindDataGrid();
 }
}

void BindDataGrid()
{

//Get incoming Month ID
strMonthID = Request.QueryString["ID"]; 

 //Start Sql statement
         strSql = "Select * From MonthInfo, Amounts, Clients";
      strSql = strSql + " where MonthInfo.MonthID = Amounts.MonthID";
         strSql = strSql + " and Amounts.ClientID = Clients.ClientID";
         strSql = strSql + " and UserName = '" + Session["UserName"] + "'";
      strSql = strSql + " and MonthInfo.MonthID = '" + strMonthID + "'";  
      strSql = strSql + " Order By AmountID";
     
                &nbs p;   
 
cmdSql = new SqlCommand(strSql, conPayment);   
conPayment.Open();
 
 
 SqlDataReader reader = cmdSql.ExecuteReader();

if (reader.HasRows) {
   dtrPayment.Visible = true;
   dtrPayment.DataSource = reader;
   dtrPayment.DataBind(); 
  
I'd like to add the code for retrieving information  from Month_Year. Look at the code that I commented out below. It didn't work.

  //MonthName = dtrPayment.Tables[0].Rows[0].Item["Month_Year"]; 
  
   //Display Month Name
   //lblMonthName.Text = "Payments For " & Month_Year; 
  
}
else
  
     dtrPayment.Visible = false;
  

//don't forget to close the reader !!!
//reader.Close();
conPayment.Close();
}

</Script>

<html>
<head><title>Amounts</title></head>
<body>


<form Runat="Server">

<asp:DataGrid
  ID="dtrPayment"
  DataKeyField="AmountID"
  AutoGenerateColumns="False"
  CellPadding="10"
  HeaderStyle-BackColor="tan"
  Runat="Server">
<Columns>

  <asp:BoundColumn
    HeaderText="Service"
    DataField="Service" />

  <asp:BoundColumn
    HeaderText="Payment Method"
    DataField="PaymentMethod" />
   
   <asp:BoundColumn
    HeaderText="Payment Owed"
    DataField="PaymentOwed"
    DataFormatString="{0:C}"/>
   
    <asp:BoundColumn
    HeaderText="Paid Date"
    DataField="PaidDate"
    DataFormatString="{0:MM/dd/yyyy}"/>  

 
</Columns>
</asp:DataGrid>

</form>
</body>
</html>




Replies:
Posted By: Mart
Date Posted: 08 August 2005 at 4:14pm
MonthName = dtrPayment["Month_Year"] as string; 
//Display Month Name
lblMonthName.Text = "Payments For " & Month_Year; 


Posted By: Misty
Date Posted: 08 August 2005 at 4:47pm
Unfortunately, that didn't work. I got the following error message: Compiler Error Message: CS0021: Cannot apply indexing with [] to an expression of type 'System.Web.UI.WebControls.DataGrid'

Source Error:

Line 51:    
Line 52: 
Line 53:    MonthName = dtrPayment["Month_Year"] as string;
Line 54:   
Line 55:   //Display Month Name


Posted By: Mart
Date Posted: 08 August 2005 at 5:03pm
oh, try

reader["Month_Year"] as string;

and you commented out the line where you close the reader, even though before that you wrote "Don't forget to close the reader!!" lol


Posted By: Misty
Date Posted: 08 August 2005 at 10:54pm
I commented out the line of code that closed the reader because it caused an error message before.
I am getting another error message that I don't understand. I tested the sql query in Query Analyzer and I got the records fine. Here's the error message:
Exception Details: System.InvalidOperationException: Invalid attempt to read when no data is present.

Source Error:

Line 51:    //MonthName = dtrPayment.Tables[0].Rows[0].Item["Month_Year"]; 
Line 52: 
Line 53:    MonthName = reader["Month_Year"] as string;
 
Here's my modified code:
 
 //Start Sql statement
         strSql = "Select * From MonthInfo, Amounts, Clients";
      strSql = strSql + " where MonthInfo.MonthID = Amounts.MonthID";
         strSql = strSql + " and Amounts.ClientID = Clients.ClientID";
         strSql = strSql + " and UserName = '" + Session["UserName"] + "'";
      strSql = strSql + " and MonthInfo.MonthID = '" + strMonthID + "'";  
      strSql = strSql + " Order By AmountID";
     
                &nbs p;   
 
cmdSql = new SqlCommand(strSql, conPayment);   
conPayment.Open();
 
 
 SqlDataReader reader = cmdSql.ExecuteReader();
if (reader.HasRows) {
   dtrPayment.Visible = true;
   dtrPayment.DataSource = reader;
   dtrPayment.DataBind();
  
   MonthName = reader["Month_Year"] as string;
 
  //Display Month Name
  lblMonthName.Text = "Payments For " + MonthName; 
 
}
else
  
     dtrPayment.Visible = false;
  
//don't forget to close the reader !!!
reader.Close();
conPayment.Close();


Posted By: Leeb65
Date Posted: 09 August 2005 at 2:40am
Quote MonthName = dtrPayment["Month_Year"] as string; 
//Display Month Name
lblMonthName.Text = "Payments For " & Month_Year; 
 
The answer sort of hits you in the face
 
MonthName = dtrPayment["Month_Year"] as string; 
//Display Month Name
lblMonthName.Text = "Payments For " + MonthName
 
you must display the variable you set not the column name.


-------------
Lee

http://www.rheinsitemedia.de">


Posted By: Mart
Date Posted: 09 August 2005 at 4:24am
That wouldn't explain the ADO error though. Try changing reader.HasRows to reader.Read().




Posted By: Misty
Date Posted: 09 August 2005 at 1:20pm
I'm still getting the error message after changing it to reader.Read(). I am not sure what is wrong. Maybe there's a typo somewhere. The SQL Query works fine.
 
Here's my modified code:
 
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page Language="C#" Debug="true" %>

 <script language=C# runat=server>
string strConnect;
SqlConnection conPayment;
SqlCommand cmdSql;
string strSql;
string strMonthID; 
string MonthName;
void Page_Load()
{
 strConnect = ConfigurationSettings.AppSettings["ConnectionString"];
 conPayment = new SqlConnection(strConnect);
 if (!(IsPostBack)) {
    BindDataGrid();
 }
}
void BindDataGrid()
{
//Get incoming Month ID
strMonthID = Request.QueryString["ID"]; 
 //Start Sql statement
         strSql = "Select * From MonthInfo, Amounts, Clients";
      strSql = strSql + " where MonthInfo.MonthID = Amounts.MonthID";
         strSql = strSql + " and Amounts.ClientID = Clients.ClientID";
         strSql = strSql + " and UserName = '" + Session["UserName"] + "'";
      strSql = strSql + " and MonthInfo.MonthID = '" + strMonthID + "'";  
      strSql = strSql + " Order By AmountID";
     
                &nbs p;   
 
cmdSql = new SqlCommand(strSql, conPayment);   
conPayment.Open();
 
 
 SqlDataReader reader = cmdSql.ExecuteReader();
if (reader.Read()) {
   dtrPayment.Visible = true;
   dtrPayment.DataSource = reader;
   dtrPayment.DataBind();
  
   MonthName = reader["Month_Year"] as string;
 
  //Display Month Name
  lblMonthName.Text = "Payments For " + MonthName;
 
  
}
else
  
     dtrPayment.Visible = false;
  
//don't forget to close the reader !!!
reader.Close();
conPayment.Close();
}
</Script>
<html>
<head><title>Amounts</title></head>
<body>
<h1><asp:Label id="lblMonthName" runat="server"/> </h1>
<form Runat="Server">
<asp:DataGrid
  ID="dtrPayment"
  DataKeyField="AmountID"
  AutoGenerateColumns="False"
  CellPadding="10"
  HeaderStyle-BackColor="tan"
  Runat="Server">
<Columns>
  <asp:BoundColumn
    HeaderText="Service"
    DataField="Service" />
  <asp:BoundColumn
    HeaderText="Payment Method"
    DataField="PaymentMethod" />
   
   <asp:BoundColumn
    HeaderText="Payment Owed"
    DataField="PaymentOwed"
    DataFormatString="{0:C}"/>
   
    <asp:BoundColumn
    HeaderText="Paid Date"
    DataField="PaidDate"
    DataFormatString="{0:MM/dd/yyyy}"/>  
 
</Columns>
</asp:DataGrid>
</form>
</body>
</html>


Posted By: Leeb65
Date Posted: 10 August 2005 at 3:00am

MonthName = reader["Month_Year"].ToString();

use that instead, that's C# the other is VB


-------------
Lee

http://www.rheinsitemedia.de">


Posted By: Mart
Date Posted: 10 August 2005 at 5:12am
That code is in C# though, so the as keyword is fine. Unless you've never met as before, it's kinda like using

(string) statement;

But it only works for reference types and is equivalent to:

statement == null ? null : (type) statement;


Posted By: Misty
Date Posted: 11 August 2005 at 2:21am
I've decided to change this to VB.Net. No one seems to be able to figure out exactly what's wrong with my C# Code.
 
I'm still having some problems. But I think it will be much easier to solve in VB.Net. I'm currently getting the error message: Compiler Error Message: BC30367: Class 'System.Web.UI.WebControls.DataGrid' cannot be indexed because it has no default property.

Source Error:

Line 52: 
Line 53:    
Line 54: MonthName = dtrPayment("Month_Year").ToString
 
Here's my VB.Net code:
 
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page Language="VB" Debug="true" %>
<Script Runat="Server">
Dim strConnect As String
Dim conPayment As SqlConnection
Dim cmdSql As SqlCommand
Dim strSql As String
Dim strMonthID As String
Dim MonthName As String
Sub Page_Load
    'Get connection string from Web.Config
    strConnect = ConfigurationSettings.AppSettings("ConnectionString")
    conPayment = New SQLConnection(strConnect) 
 
 
  If Not IsPostBack Then
    BindDataGrid  
  
    
  End If
End Sub
Sub BindDataGrid
'Get incoming Month ID
strMonthID = request.params("ID")
         'Start Sql statement
         strSql = "Select * From MonthInfo, Amounts, Clients"
      strSql = strSql & " where MonthInfo.MonthID = Amounts.MonthID"
         strSql = strSql & " and Amounts.ClientID = Clients.ClientID"
         strSql = strSql & " and UserName = '" & Session("UserName") & "'"
      strSql = strSql & " and MonthInfo.MonthID = '" & strMonthID & "'"  
      strSql = strSql & " Order By AmountID"
     
                &nbs p;   
 
cmdSql = new SqlCommand(strSql, conPayment)   
conPayment.Open()
 
 
 
 
'if(dtrPayment.HasRows) - Needs Help With Changing This. I would like to find out if a record exists or not.  
   'dtrPayment.Visible = true
   dtrPayment.DataSource = cmdSql.ExecuteReader()
   dtrPayment.DataBind()
  
MonthName = dtrPayment("Month_Year").ToString -The error message
 
  
  'Display Month Name
  'lblMonthName.Text = "Payments For " & MonthName 

'else
  
     'dtrPayment.Visible = false
  
'don't forget to close the reader !!!
'dtrPayment.Close()
conPayment.Close()
'end if
End Sub
</Script>
<html>
<head><title>Amounts</title></head>
<body>
<h1><asp:Label id="lblMonthName" runat="server"/> </h1>
<form Runat="Server">
<asp:DataGrid
  ID="dtrPayment"
  DataKeyField="AmountID"
  AutoGenerateColumns="False"
  CellPadding="10"
  HeaderStyle-BackColor="tan"
  Runat="Server">
<Columns>
  <asp:BoundColumn
    HeaderText="Service"
    DataField="Service" />
  <asp:BoundColumn
    HeaderText="Payment Method"
    DataField="PaymentMethod" />
   
   <asp:BoundColumn
    HeaderText="Payment Owed"
    DataField="PaymentOwed"
    DataFormatString="{0:C}"/>
   
    <asp:BoundColumn
    HeaderText="Paid Date"
    DataField="PaidDate"
    DataFormatString="{0:MM/dd/yyyy}"/>  
 
</Columns>
</asp:DataGrid>
</form>
</body>
</html>
 
 



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net