public partial class FrmOnline : System.Web.UI.Page
{
    string Query;
    SqlCommand cmd;
    SqlConnection conn;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
                     OpenSQLConnection();
           SqlCommand cmd = new SqlCommand("select * from Info1 where UID=1", conn);
           SqlDataReader dr;
           dr = cmd.ExecuteReader();
           while (dr.Read())
           {
               dr.Read();
               TextBox1.Text = dr[0].ToString();
           }
            
        }
    }


 private void OpenSQLConnection()
    {
        try
        {

            conn = new SqlConnection("Data Source=(local);Initial catalog=sonia;User ID=sonia;Password=sonia;");
            conn.Open();

        }
        catch (Exception ex)
        {
            
        }

    }

I m getting error in line TextBox1.Text = dr[0].ToString();
Invalid attempt to read when no data is present.

But the record is there in DB,of UID 1.then y the error is coming.

Dani AI

Generated

The error "Invalid attempt to read when no data is present" means the SqlDataReader is not sitting on a valid row when you try to access columns. In the code posted by the reader is advanced twice (once in the while (dr.Read()) condition and again inside the loop), so a single-row result is skipped and dr[0] runs when there is no current row. was right to suggest checking for rows and only calling Read() once.

A safer, simpler pattern when you only need one column is to use a parameterized query and ExecuteScalar, and always dispose connections/commands/readers with using so resources are closed even on error. Also avoid empty catch blocks (log or surface the exception), check for DBNull.Value, and keep SQL in config rather than hardcoding credentials.

Example pattern (adapt column and connection-string name to your app):

string connString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand("SELECT TOP 1 ColumnName FROM Info1 WHERE UID = @uid", conn))
{
    cmd.Parameters.AddWithValue("@uid", 1);
    conn.Open();
    object val = cmd.ExecuteScalar();
    TextBox1.Text = (val == null || val == DBNull.Value) ? string.Empty : val.ToString();
}

Extra notes: verify the correct column name and that UID really matches a row with SSMS; if you need multiple columns use a single if (reader.Read()) then read columns by name and check IsDBNull before converting; never swallow exceptions silently; and avoid hardcoded credentials in the connection string. These steps will remove the read-position bug and make the code more robust.

public partial class FrmOnline : System.Web.UI.Page
{
    string Query;
    SqlCommand cmd;
    SqlConnection conn;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
                     OpenSQLConnection();
           SqlCommand cmd = new SqlCommand("select * from Info1 where UID=1", conn);
           SqlDataReader dr;
           dr = cmd.ExecuteReader();
           while (dr.Read())
           {
               dr.Read();
               TextBox1.Text = dr[0].ToString();
           }
            
        }
    }


 private void OpenSQLConnection()
    {
        try
        {

            conn = new SqlConnection("Data Source=(local);Initial catalog=sonia;User ID=sonia;Password=sonia;");
            conn.Open();

        }
        catch (Exception ex)
        {
            
        }

    }

I m getting error in line TextBox1.Text = dr[0].ToString();
Invalid attempt to read when no data is present.

But the record is there in DB,of UID 1.then y the error is coming.

hai,
Rewrite the code like this and try again

if (!IsPostBack)
{
OpenSQLConnection();
SqlCommand cmd = new SqlCommand("select * from Info1 where UID=1", conn);

SqlDataReader dr;
try
{
    dr = cmd.ExecuteReader();
if(dr.HasRows)
{
dr.Read();
TextBox1.Text = dr[0].ToString();
}
}
}
catch(Exception){}
finally
{
if(dr!=null) dr.Close();
}
}
}

update me with the result.

Thanks,
Shenu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.