Invalid attempt to read when no data is present
I want to creat dynamic <asp:table ID=""...>
and I want to have a totalAmount
column by reading the first cell from my datareader and put it into total, this column must show the cumulative numbers....
try
{
sqlconnection();
sqlcommand("", sqlconnection);
sqlconnection.open();
sqldatereader dr;
dr = sqlcommand.executereader();
if (dr.HasRows)
{
//create column names;
开发者_开发知识库 table_ShowReport.Rows.Add(new TableRow());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = "Amount";
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = "Total";
//here I wantend to get the initial value to my total:
double total = double.parse(dr["amount"].tostring());
while (dr.Read())
{
table_ShowReport.Rows.Add(new TableRow());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = dr["amount"].tostring();
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = total.tostring();
total += double.parse(dr["amount"].tostring());
}
}
sqlconnection.close();
}
catch (exception ex)
{
respose.write(ex.message);
}
but it shown me this error:
Invalid attempt to read when no data is present.
Please help me. Thanks
Tanya
You must call dr.Read()
before you attempt to read from the SqlDataReader.
I suggest initializing your total
variable with zero before reading the rows. Then set the total
with the updated amount after calling dr.Read()
but before displaying the total amount. Like this:
double runningTotal = 0d;
while (dr.Read())
{
table_ShowReport.Rows.Add(new TableRow());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = dr["amount"].ToString();
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Add(new TableCell());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].BorderWidth = 2;
runningTotal += double.Parse(dr["amount"].ToString());
table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells[table_ShowReport.Rows[table_ShowReport.Rows.Count - 1].Cells.Count - 1].Text = runningTotal.ToString();
}
}
sqlconnection.Close();
Here is the complete code refactored a little bit so it's more clear:
private static void PopulateReport(Table table, string connection)
{
table.Rows.Clear();
PopulateReportRow(table, "Amount", "Total");
string commandText = @"...";
using (SqlConnection conn = new SqlConnection(connection))
{
conn.Open();
using (SqlCommand comm = new SqlCommand(commandText, conn))
{
using (SqlDataReader reader = comm.ExecuteReader())
{
double total = 0d;
while (reader.Read())
{
double amount = (double)reader["amount"];
total += amount;
PopulateReportRow(table, amount.ToString("N"), total.ToString("N"));
}
}
}
}
}
private static void PopulateReportRow(Table table, string amountValue, string totalValue)
{
const int cellBorderWidth = 2;
int rowIndex = table.Rows.Add(new TableRow());
TableCell amountCell = new TableCell();
table.Rows[rowIndex].Cells.Add(amountCell);
amountCell.BorderWidth = cellBorderWidth;
amountCell.Text = amountValue;
TableCell totalCell = new TableCell();
table.Rows[rowIndex].Cells.Add(totalCell);
totalCell.BorderWidth = cellBorderWidth;
totalCell.Text = totalValue;
}
You're trying to read from the reader before you get to the first row.
You should set it to 0
and populate it in the loop s you're doing now.
protected void Btnlogin_Click(object sender, EventArgs e)
{
SqlConnection con=newSqlConnection("connection string");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from tbluser where username='" + txtusername + "' and passworde='" + txtpassworde + "'";
cmd.Connection = con;
if (con.State == ConnectionState.Closed) con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr["username"] == txtusername.Text.Trim() && dr["passworde"] == txtpassworde)
{
Response.Redirect("kala.aspx");
return;
}
}
}
Code is correct but does not enter the next page
精彩评论