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>