Error is:
System.InvalidOperationException: Invalid attempt to read when no data is present. at System.Data.SqlClient.SqlDataReader.ReadColumn(Int32 i, Boolean setTimeout) at System.Data.SqlClient.SqlDataReader.GetString(Int32 i) at Login.userLogin_Click(Object sender, EventArgs e) in d:\TEST\attend\Login.aspx.cs:line 47
but i declare a value in that field but still get error here., advance thanks..
Sunny SharmaPosted Jun 3, 2013, 5:08 AM
The Exception you showed says that it occured because DataReader didn't find any data in that Row[Column] you tried to access. the best way to track it down is to use try...catch block, so that you can see what actually is happening.
do it like this:
----------------
while(reader.Read()) // Note that execution will enter this loop only if has rows, means you don't need to check reader.HasRows
{
try
{
label4.Text = reader.GetString(11);
}
catch(Exception ex)
{
Consolle.WriteLine("Error in communication: "+ex.Message); // You're free to Show Message or render the message to console or elsewhere.
}
}
-----------------------
Hope it helps :)
Thanks
BalaPosted Jun 3, 2013, 8:07 AM
Ravi ShekharPosted Jun 3, 2013, 5:24 AM
use while(dr.read())
{
label4.text = dr.GetString(11);
}
if it doesnot help you. please share your complete code.
Jignesh TrivediPosted Jun 3, 2013, 4:53 AM
hi,
It might be possible data reader hasn't any value
if (reader.HasRows)
{
while (reader.Read())
{
string userid = Convert.ToString(dataReader["UserId"]);
}
}
hope this will help you.
Sandeep Singh ShekhawatPosted Jun 3, 2013, 4:41 AM
while(dr.Read())
{
Kiresh GangariyaPosted Jun 3, 2013, 4:20 AM
{
(dataReader.Read() == true)
string userid = Convert.ToString(dataReader["UserId"]);}