开发者

InvalidOperationException: Invalid attempt to read when no data is present. (SQL)

开发者 https://www.devze.com 2023-03-12 23:02 出处:网络
void ExecuteContent() { StringBuilder sb = new StringBuilder(); sb.Append(\"SELECT TOP 1 @UsersI开发者_JS百科D,u.Avatar,t.Date, u.Name, t.ThreadTitle, t.ThreadParagraph\");
    void ExecuteContent()
{
    StringBuilder sb = new StringBuilder();
    sb.Append("SELECT TOP 1 @UsersI开发者_JS百科D,u.Avatar,t.Date, u.Name, t.ThreadTitle, t.ThreadParagraph");
     sb.Append(" FROM Users as u");
     sb.Append(" INNER JOIN Threads as t ON u.UsersID = t.UsersID");
     sb.Append(" Where @UsersID=t.UsersID");
     sb.Append(" ORDER BY t.Date DESC");


     using (SqlConnection conn = new SqlConnection(AnswerQuestion.connectionString))
     {
         conn.Open();
         SqlCommand sqlComm = new SqlCommand(sb.ToString(), conn);
        MembershipUser CurrentUser = Membership.GetUser();    
         Guid i = (Guid)CurrentUser.ProviderUserKey;
         sqlComm.Parameters.Add("@UsersID", SqlDbType.UniqueIdentifier).Value = i;
         SqlDataReader dr = sqlComm.ExecuteReader();

         UserName = dr["Name"].ToString();//The exception is thrown here
         Image = (Image) dr["Avatar"];
         ThreadTitle = dr["ThreadTitle"].ToString();
         ThreadParagraph = dr["ThreadParagraph"].ToString();
         Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString()); 
     }

}

I dont understand why I get it. All i am trying to do is to get the last person who posted the thread.. I looked at debugging, and things seem fine. I also looked at the sql server in visual studio 2010.. there is data,,but somehow it is not being read and an exception is thrown... :(


You should check for dr.Read() before accessing the values:

if(dr.Read())
{
    UserName = dr["Name"].ToString();//The exception is thrown here
    Image = (Image) dr["Avatar"];
    ThreadTitle = dr["ThreadTitle"].ToString();
    ThreadParagraph = dr["ThreadParagraph"].ToString();
    Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString());
}


You must loop trough your reader:

while(dr.Read())
{
    serName = dr["Name"].ToString();//The exception is thrown here
         Image = (Image) dr["Avatar"];
         ThreadTitle = dr["ThreadTitle"].ToString();
         ThreadParagraph = dr["ThreadParagraph"].ToString();
         Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString()); 
}

or if(dr.Read()) if you only have one result

And You should always surround you Reader with a using:

using(dr)
{
    while(dr.Read())
    {
    }
}

And you should do some null check before you call ToString();

ThreadParagraph = dr["ThreadParagraph"] == null ? "" : dr["ThreadParagraph"].ToString();

And here's a little tip to write queries in code, use verbatim:

var query = @"SELECT TOP 1 @UsersID,u.Avatar,t.Date, u.Name, t.ThreadTitle, t.ThreadParagraph 
    FROM Users as u 
    INNER JOIN Threads as t ON u.UsersID = t.UsersID 
    Where @UsersID=t.UsersID ORDER BY t.Date DESC";
0

精彩评论

暂无评论...
验证码 换一张
取 消