Exception while assigning cmd.ExecuteReader() to a variable

Nov 3 2016 2:44 AM
Hi,
 Below is my codding.I am trying to take the loggedInUsers while chatting.
But while I am assigning cmd.ExecuteReader() to a variable its throws Exception.
 
private void GetLoggedInUsers()
{
string cs = ConfigurationManager.ConnectionStrings["LinqChatConnectionString"].ConnectionString;
try
{
using (SqlConnection con = new SqlConnection(cs))
{
// let's check if this authenticated user exist in the
// LoggedInUser table (means user is logged-in to this room)
SqlCommand cmd = new SqlCommand("GetLoggedInUsersByUserIDandRoomID", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UserID", Convert.ToInt32(Session["ChatUserID"]));
cmd.Parameters.AddWithValue("@RoomID", Convert.ToInt32(lblRoomId.Text));
con.Open();

SqlDataReader sdr = cmd.ExecuteReader();
var user = sdr;
// if user does not exist in the LoggedInUser table
// then let's add/insert the user to the table
if (user==null)
{
SqlCommand cmd1 = new SqlCommand("InsertLoggedInUser", con);
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@UserID", Convert.ToInt32(Session["ChatUserID"]));
cmd1.Parameters.AddWithValue("@RoomID", Convert.ToInt32(lblRoomId.Text));
cmd1.ExecuteNonQuery();
}
sdr.Close();
con.Close();
}
}
catch (Exception ex)
{ }
string userIcon;
StringBuilder sb = new StringBuilder();

// get all logged in users to this room
try
{
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd3 = new SqlCommand("GetLoggedInUsersByRoomID", con);
cmd3.CommandType = CommandType.StoredProcedure;
cmd3.Parameters.AddWithValue("@RoomID", Convert.ToInt32(lblRoomId.Text));
con.Open();

SqlDataReader sdr2 = cmd3.ExecuteReader();
var loggedInUsers = sdr2;

foreach (var loggedInUser in loggedInUsers)
{
SqlCommand cmd2 = new SqlCommand("GetUserByUserID",con);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("@UserID", loggedInUsers["UserID"]);
sdr2.Close();
SqlDataReader sdr3 = cmd2.ExecuteReader();
var getuser = sdr3;

// show user icon based on sex
if (getuser["Sex"].ToString().ToLower() == "m")//Throwing Exception here
userIcon = "<img src='Images/manIcon.gif' style='vertical-align:middle' alt=''> ";
else
userIcon = "<img src='Images/womanIcon.gif' style='vertical-align:middle' alt=''> ";

// open the chat window when the logged-in user clicks another user in the list
if (getuser["Username"].ToString() != (string)Session["ChatUsername"])
{
sb.Append(userIcon + "<a href=# onclick=\"window.open('ChatWindow.aspx?FromUserId=" + Session["ChatUserID"] +
"&ToUserId=" + getuser["UserID"] + "&Username=" + getuser["Username"] +
"','','width=400,height=200,scrollbars=no,toolbars=no,titlebar=no,menubar=no'); isLostFocus = 'true';\">" +
getuser["Username"] + "</a><br>");
}
else
{
sb.Append(userIcon + "<b>" + getuser["Username"] + "</b><br>");
}
}
// holds the names of the users shown in the chatroom
litUsers.Text = sb.ToString();
}
}
catch(Exception ex)
{
}
}
 
The Exception is "Invalid attempt to read when no data is present.".How to overcome this?
 
Thanks,
Poornima

Answers (2)