Dear Sir,
I have a login table having column as 'Status' which is containing value "online" and offline.
and I set by default "Status=Offline".
When I have login successfully then status is updated as "online". and on logout "Status=Offline" updated.
But when Application through an exception or terminated Then "Status=online" shown in login table.
so please tell me how to resolve this type of issue in C#
Bikesh SrivastavaPosted Jan 3, 2017, 1:15 PM
Amit GuptaPosted Jan 3, 2017, 6:34 AM
adam gillsPosted Jan 3, 2017, 5:42 AM
try
{
mycon = con.GetConnection();
DateTime userlogout = zone1.ToLocalTime(DateTime.Now);
string logoutdt = userlogout.ToString("yyyy-MM-dd");
string logouttm = userlogout.ToString("HH:mm:ss tt");
string getuser = FrmUserLogin.SetValueForusername;
DialogResult dResult = MessageBox.Show("Are you sure want to logout!", "Logout", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dResult == DialogResult.Yes)
{
string qry = "(select * from login where username='" + getuser.Trim() + "') ";
cmd = new SqlCommand(qry, mycon);
mycon.Open();
SqlDataReader dr2 = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr2);
string getstatus = dt.Rows[0]["workstatus"].ToString();
dr2.Close();
dt.Clear();
string setworkstatus = "offline";
if (getstatus == "online")
{
int lastlogid = FrmUserLogin.lastloginid;
string qrytimediff = "select * from userloginfo where dtid='" + lastlogid + "' and username='" + getuser.Trim() + "'";
SqlCommand diffcmd = new SqlCommand(qrytimediff, mycon);
SqlDataReader ipdr = diffcmd.ExecuteReader();
DataTable ipdt = new DataTable();
ipdt.Load(ipdr);
string getlointm = ipdt.Rows[0]["logintm"].ToString();
ipdr.Close();
ipdt.Clear();
TimeSpan timeDiff;
timeDiff = DateTime.Parse(logouttm) - DateTime.Parse(getlointm);
string logoutuser = "Update login set workstatus='" + setworkstatus.Trim() + "',logouttime='" + logouttm.Trim() + "' where username='" + getuser.Trim() + "'";
string logoutlog = "Update userloginfo set logoutdt='" + logoutdt.Trim() + "',logouttm='" + logouttm.Trim() + "',timediff='" + timeDiff + "' where username='" + getuser.Trim() + "' and dtid='" + lastlogid + "'";
cmd = new SqlCommand(logoutuser, mycon);
SqlCommand cmdlog = new SqlCommand(logoutlog, mycon);
int exc1 = cmd.ExecuteNonQuery();
int exc2 = cmdlog.ExecuteNonQuery();
mycon.Close();
//mycon.Dispose();
if (exc1 == 1 && exc2 == 1)
{
string workinghours = "SELECT convert(time(7), dateadd(second, SUM(DATEDIFF(SECOND, logintm, logouttm)), 0), 108) as wh " +
"FROM userloginfo where username='" + getuser.Trim() + "' and logoutdt='" + logoutdt.Trim() + "' group by username,logindt";
mycon.Open();
SqlCommand whcmd = new SqlCommand(workinghours, mycon);
SqlDataReader whdr = whcmd.ExecuteReader();
DataTable whdt = new DataTable();
whdt.Load(whdr);
string currentwh = whdt.Rows[0]["wh"].ToString();
whdr.Close();
whdt.Clear();
mycon.Close();
string mymsg = "Your today working hours is";
MessageBox.Show(mymsg + " " + currentwh, "Working Hours", MessageBoxButtons.OK, MessageBoxIcon.Information);
// whdt.Reset();
//Application.Exit();
System.Environment.Exit(0);
}
else
{
return;
}
}
}
else if (dResult == DialogResult.No)
{
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
mycon.Close();
mycon.Dispose();
}
}
adam gillsPosted Jan 3, 2017, 5:40 AM
DateTime indianTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, INDIAN_ZONE);
DateTime localTime = DateTime.Now;
DateTime userlogindt = logindatetime.ToLocalTime(DateTime.Now);
string logindate = indianTime.Date.ToString("yyyy-MM-dd");
string logintime = indianTime.ToString("HH:mm:ss tt");
if (txtloginuser.Text.Trim()=="" || txtloginpass.Text.Trim() =="")
{
MessageBox.Show("Please fill user id or password !", "Login",MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
txtloginuser.Focus();
return;
}
try
{
string qry = "(select * from login where username='" + txtloginuser.Text.Trim() + "' and pass='" + txtloginpass.Text.Trim() + "') ";
cmd = new SqlCommand(qry, mycon);
mycon.Open();
sda = new SqlDataAdapter(cmd);
mycon.Close();
DataTable dt = new DataTable();
//cmd = new SqlCommand(qry,mycon);
//sda = new SqlDataAdapter(cmd);
//dt.Load(dr2);
sda.Fill(dt);
cmd.Dispose();
if (dt.Rows.Count == 1)
{
string loginstatus = dt.Rows[0]["status"].ToString();
if (loginstatus != "Active")
{
MessageBox.Show("User ID '" + txtloginuser.Text + "' " + "is not activated !", "Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtloginuser.Text = "";
txtloginpass.Text = "";
txtloginuser.Focus();
mycon.Close();
this.Show();
return;
}
string logintype = dt.Rows[0]["logintype"].ToString();
if (logintype != "User")
{
MessageBox.Show("Invalid login type!", "Login Type", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtloginuser.Text = "";
txtloginpass.Text = "";
txtloginuser.Focus();
mycon.Close();
this.Show();
return;
}
string loginuser = dt.Rows[0]["username"].ToString();
string userpwd = dt.Rows[0]["pass"].ToString();
string lastlogindate = dt.Rows[0]["logindate"].ToString();
string yesterdaydate;
if (lastlogindate == "")
{
yesterdaydate = "";
}
else
{
yesterdaydate = Convert.ToDateTime(lastlogindate).ToString("yyyy-MM-dd");
}
// lastdate = (firstlogindate.ToString("yyyy-MM-dd")).ToString();
if (loginuser == txtloginuser.Text.Trim() || userpwd == txtloginpass.Text.Trim())
{
GetValueForworkstatus = dt.Rows[0]["workstatus"].ToString();
getfullname = dt.Rows[0]["name"].ToString();
SetValueForusername = txtloginuser.Text;
// MessageBox.Show("Thank you for activation!");
if (GetValueForworkstatus == "offline")
{
if(yesterdaydate =="")
{
string logoutuser1 = "Update login set workstatus='online',logindate='" + logindate + "' ,logintime='" + logintime.Trim() + "' " +
"where username='" + txtloginuser.Text + "'";
cmd = new SqlCommand(logoutuser1, mycon);
mycon.Open();
cmd.ExecuteNonQuery();
mycon.Close();
}
else if (yesterdaydate != logindate)
{
string logoutuser1 = "Update login set workstatus='online',logindate='" + logindate + "',logintime='" + logintime.Trim() + "' " +
" where username='" + txtloginuser.Text + "'";
cmd = new SqlCommand(logoutuser1, mycon);
mycon.Open();
cmd.ExecuteNonQuery();
mycon.Close();
}
else if(yesterdaydate == logindate)
{
string logoutuser2 = "Update login set workstatus='online',logindate='" + logindate + "' where username='" + txtloginuser.Text + "'";
cmd = new SqlCommand(logoutuser2, mycon);
mycon.Open();
cmd.ExecuteNonQuery();
mycon.Close();
}
//else
//{
// string logoutuser2 = "Update login set workstatus='online',logindate='" + logindate + "' where username='" + txtloginuser.Text + "'";
// cmd = new SqlCommand(logoutuser2, mycon);
// mycon.Open();
// cmd.ExecuteNonQuery();
// mycon.Close();
//}
string loginfo = "Insert into userloginfo(username,logindt,logintm)values('" + txtloginuser.Text + "','" + logindate + "','" + logintime.Trim() + "');SELECT CAST(SCOPE_IDENTITY()as int)";
SqlCommand cmd1 = new SqlCommand(loginfo,mycon);
mycon.Open();
lastloginid = (int)cmd1.ExecuteScalar();
dt.Clear();
mycon.Close();
this.Hide();
FrmUserPanel fup = new FrmUserPanel();
fup.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("User is online yet !", "User Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtloginuser.Text = "";
txtloginpass.Text = "";
txtloginuser.Focus();
mycon.Close();
return;
}
}
}
else
{
MessageBox.Show("Invalid userid or password!", "User Login", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtloginuser.Text = "";
txtloginpass.Text = "";
txtloginuser.Focus();
mycon.Close();
this.Show();
return;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
mycon.Close();
}
Amit GuptaPosted Jan 3, 2017, 5:05 AM