Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Heading Above The DataGrid
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Heading Above The DataGrid

 Post Reply Post Reply Page  12>
Author
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Topic: Heading Above The DataGrid
    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>

Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 08 August 2005 at 4:14pm
MonthName = dtrPayment["Month_Year"] as string; 
//Display Month Name
lblMonthName.Text = "Payments For " & Month_Year; 
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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();
Back to Top
Leeb65 View Drop Down
Groupie
Groupie


Joined: 05 December 2003
Location: Germany
Status: Offline
Points: 62
Post Options Post Options   Thanks (0) Thanks(0)   Quote Leeb65 Quote  Post ReplyReply Direct Link To This Post 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.


Edited by Leeb65 - 09 August 2005 at 2:40am
Lee


Back to Top
Mart View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 November 2002
Status: Offline
Points: 2304
Post Options Post Options   Thanks (0) Thanks(0)   Quote Mart Quote  Post ReplyReply Direct Link To This Post Posted: 09 August 2005 at 4:24am
That wouldn't explain the ADO error though. Try changing reader.HasRows to reader.Read().


Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post 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>
Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.