Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need help as I had create the registration page for the users and everything goes smooth until they encounter the error of

"Invalid Attempt to read when no data is presented"
whenever submit.


I keep on track the bugs and fix it and still not able fix as encounter another error like "connection needed to close" or etc. Below are my original code and need help:

Error Appeared:

Server Error in "/" Application.

Stack Trace: [InvalidOperationException: Invalid attempt to read when no data is present.] System.Data.SqlClient.SqlDataReader.CheckDataisReady(Int32 columnIndex, Boolean allowPartiallyReadColum, Boolean PermitAsync, string methodName) +6531242

System.Data.SqlClient.SqlDataReader.TryReadColumn(Int32 i, Boolean setTimeout, Boolean allowPartiallyReadColumn) +81

System.Data.SqlClient.SqlDataReader.GetValueInternal(Int32 i) +43

System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) +151

vnetwork.Account.DatabaseEntry.Page_Load(Object sender, EventArgs e) +432

System.Web.UI.Control.LoadRecursive() +71

System.Web.UI.Page.ProcessRequestmain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178


Remarks: User told me some times is leave it open the page awhile then whenever try to key in and submit and error above shown and also sometimes trying to key in 2nd time data.

What I have tried:

C#
public partial class DatabaseEntry : System.Web.UI.Page
{

public string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;

protected void Page_Load(object sender, EventArgs e)
{



SqlConnection con = new SqlConnection(strConnString);

con.Open();
str = ("select * from AspNetUsers where UserName = ('" + Session["UserName"] + "')");
com = new SqlCommand(str, con);
object obj = Session["UserName"];

SqlDataReader reader = com.ExecuteReader();
reader.Read();

Label1.Text = reader["UserName"].ToString();


if (!this.IsPostBack)
{
this.BindGrid();
}
}
}
Posted
Updated 28-Feb-16 20:42pm
v3

1 solution

According to stack trace, the error is in vnetwork.Account.DatabaseEntry.Page_Load method when you are trying to get data from reader. The code you have posted is kind of irrelevant. You should check this method where you are probably trying to get data from the column that is not returned by the query.

Update:

The error is because either there is no column called UserName returned from your query or there is no data returned from the query. A better way to get data from reader is:

C#
if (reader.HasRows)
{
    while (reader.Read())
    {
    // Get data here
    }
}


Now, there are few issues with the code you have here. It is not really related to the question. These may be really irrelevant in case the code posted is just for sample.

1. Looks like you already have UserName in Session object. So why are you even running this query.
2. In case the user name in database is something else, you will need to use query. Now, if you only need the name, your SQL command should return on the name column.
3. Cache the user name in memory using a suitable method. This way you do not need to run this query on every postback.
4. You should ensure to close the SqlConnection. Take a look at using code block. It will come handy.
5. Make use of paramterized queries rather than using string concatenation.
 
Share this answer
 
v2
Comments
VandrenSKL 29-Feb-16 2:34am    
Hi, is it the source code tab design in visual that you mention?
dan!sh 29-Feb-16 2:44am    
Code should be posted in question. In comment section, it is not readable. I have updated your question with code.
dan!sh 29-Feb-16 2:58am    
Take a look at updated response.
VandrenSKL 7-Mar-16 3:47am    
Yeah as so far, is worked as so far i thought needed to modify at button there but actually is page load. thanks guys

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900