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 = "
";
else
userIcon = "
";
// 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 + " "&ToUserId=" + getuser["UserID"] + "&Username=" + getuser["Username"] +
"','','width=400,height=200,scrollbars=no,toolbars=no,titlebar=no,menubar=no'); isLostFocus = 'true';\">" +
getuser["Username"] + "
");
}
else
{
sb.Append(userIcon + "" + getuser["Username"] + "
");
}
}
// holds the names of the users shown in the chatroom
litUsers.Text = sb.ToString();
}
}
catch(Exception ex)
{
}
}
{
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 = "
else
userIcon = "
// 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 + " "&ToUserId=" + getuser["UserID"] + "&Username=" + getuser["Username"] +
"','','width=400,height=200,scrollbars=no,toolbars=no,titlebar=no,menubar=no'); isLostFocus = 'true';\">" +
getuser["Username"] + "
");
}
else
{
sb.Append(userIcon + "" + getuser["Username"] + "
");
}
}
// 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
Manas MohapatraPosted Nov 3, 2016, 3:22 AM
You shoudn't do like assigning Dataredaer to another variable.
Do iterate over the reader using Read() like below:
SqlDataReader sdr3 = cmd2.ExecuteReader();
if(sdr3.HasRows)
{
while (sdr3.Read())
{
Console.WriteLine(sdr3["Sex"].ToString());
}
}
Secondly you are executing the SQL command again and again.
Please try to retrive data at asingle shot.
poornima sekarPosted Nov 7, 2016, 2:03 AM