I am facing a peculiar issue in my ADO.NET code. This is the table data that I am accessing from a repeater from the frontend.
1 get car cleaned 2012-02-14 08:32:25.643 NULL
2 submit tax documents 2012-02-14 08:33:04.610 NULL
3 photo copy all documents 2012-02-14 08:33:04.610 NULL
Data in the first row is not being displayed at all.
If I delete the rows 2 and 3, no data is being displayed in the repeater. I think the issue is with my ADO.NET code. Also, if I truncate the table completely, the page is loading forever as opposed to displaying the “No Data Found” message in the label.
protected void Page_Load(object sender, EventArgs e)
{
txtNewTask.Focus();
if (!IsPostBack)
{
GetTaskList();
}
}
protected void GetTaskList()
{
conn = new SqlConnection(cstr);
getTasksCmd = new SqlCommand("select Name, CreationDate, CompletionDate from tasks", conn);
try
{
using (conn)
{
conn.Open();
using (reader = getTasksCmd.ExecuteReader())
{
while (!reader.Read())
{
lblDbMsg.Text = "No Data Found!";
}
rptTaskList.DataSource = reader;
rptTaskList.DataBind();
}
}
}
catch (Exception)
{
throw;
}
}
Take out the
whileloop from your code:Because you are calling
SqlDataReader.Read()once, you are moving past the first record. Therefore, if you want to be able to retrieve all of the rows of data including the first, don’t callRead()at all.SqlDataReader.Read() Method MSDN Reference