Satheesh Palani

Satheesh Palani

  • NA
  • 333
  • 79.5k

Invalid attempt to read when no data is present.

Mar 30 2016 3:04 AM
Hi,
 
   I tried to Retrieving Data from SQL by using the below code. But i am getting the following error Invalid attempt to read when no data is present. Try to help resolve my problem.
 
 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace wenservdemo
{
public partial class Testpage1 : System.Web.UI.Page
{
protected void Read_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(connection string);
string str;
SqlCommand com;
con.Open();
str = "select * from tsttable1 where FName='" + FName.Text + "'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
reader.Read();
FName.Text = reader["FName"].ToString();
reader.Read();
LName.Text = reader["LName"].ToString();
reader.Read();
Company.Text = reader["Company"].ToString();
reader.Read();
Position.Text = reader["Position"].ToString();
reader.Close();
con.Close();
}
}
}
                                                                

Answers (7)