Hi
In below code while dr.read i have given wrong value docentr . Error message should get displayed and quit the application.
docentry = (rdr["docentr"]).ToString();
Function is being called from here
C#
if (!Page.IsPostBack)
{
if (Common.CommonFunction.AuthenticatedUser().Item1) // Check if the user is authenticated
{
hdfLoginId.Value = Common.CommonFunction.GetUserInfo().Item1;
var userId = Common.CommonFunction.GetUserInfo().Item2;
var userRole = Common.CommonFunction.GetUserInfo().Item3;
GetData();
}
}
public static Tuple GetUserInfo()
{
try
{
//1 - Docentry
//2 - User ID
//3 - User Role
//4 - User Active
using (SqlConnection con = new SqlConnection(Common.CommonFunction.cnn_Live))
{
SqlCommand cmd = new SqlCommand("select userid from users where userid = '" + (AuthenticatedUser().Item3) + "'", con);
cmd.CommandType = CommandType.Text;
con.Open();
string userid = "";
object result = cmd.ExecuteScalar();
if (result != null)
{
userid = result.ToString();
SqlCommand cmd0 = new SqlCommand("select top 1 * from users where userid = '" + userid + "'", con);
cmd0.CommandType = CommandType.Text;
string docentry = "";
string userrole = "";
string status = "";
SqlDataReader rdr = cmd0.ExecuteReader();
while (rdr.Read())
{
docentry = (rdr["docentr"]).ToString();
userrole = rdr["UserRole"].ToString();
status = rdr["Status"].ToString();
}
return Tuple.Create(docentry,userid, userrole, Convert.ToBoolean(status));
}
else
{
return Tuple.Create(string.Empty,string.Empty, "User", false);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return Tuple.Create(string.Empty, string.Empty, string.Empty, false);
}
}Thanks

Gowtham CpRank #1,552 · 1K reputationPosted Aug 21, 2024, 11:52 AM EDT
Sorry for the mix-up. Using
Environment.Exit(1)will shut down the entire server, which isn't ideal for web apps. Instead, you should handle errors by redirecting users to an error page or displaying an error message.1. Update
GetUserInfo:public static Tuple GetUserInfo() { try { using (var con = new SqlConnection(Common.CommonFunction.cnn_Live)) { var cmd = new SqlCommand("SELECT userid FROM users WHERE userid = @userid", con); cmd.Parameters.AddWithValue("@userid", AuthenticatedUser().Item3); con.Open(); var userid = (string)cmd.ExecuteScalar(); if (string.IsNullOrEmpty(userid)) throw new Exception("User not found."); var cmd0 = new SqlCommand("SELECT TOP 1 * FROM users WHERE userid = @userid", con); cmd0.Parameters.AddWithValue("@userid", userid); using (var rdr = cmd0.ExecuteReader()) { if (rdr.Read()) { var docentry = (rdr["docentr"]).ToString(); if (string.IsNullOrWhiteSpace(docentry)) throw new Exception("Invalid document entry value."); var userrole = rdr["UserRole"].ToString(); var status = rdr["Status"].ToString(); return Tuple.Create(docentry, userid, userrole, Convert.ToBoolean(status)); } else { throw new Exception("No user data found."); } } } } catch (Exception ex) { HttpContext.Current.Response.Redirect("~/ErrorPage.aspx?errorMessage=" + HttpUtility.UrlEncode(ex.Message)); return Tuple.Create(string.Empty, string.Empty, string.Empty, false); } }Error Page :
Gowtham CpRank #1,552 · 1K reputationPosted Aug 21, 2024, 9:33 AM EDT
Replace your
GetUserInfomethod with this:public static Tuple GetUserInfo() { try { using (SqlConnection con = new SqlConnection(Common.CommonFunction.cnn_Live)) { SqlCommand cmd = new SqlCommand("select userid from users where userid = '" + AuthenticatedUser().Item3 + "'", con); cmd.CommandType = CommandType.Text; con.Open(); string userid = (string)cmd.ExecuteScalar(); if (string.IsNullOrEmpty(userid)) { MessageBox.Show("User not found. Exiting application."); Environment.Exit(1); } SqlCommand cmd0 = new SqlCommand("select top 1 * from users where userid = '" + userid + "'", con); SqlDataReader rdr = cmd0.ExecuteReader(); if (rdr.Read()) { string docentry = (rdr["docentr"]).ToString(); if (string.IsNullOrWhiteSpace(docentry)) { MessageBox.Show("Invalid document entry value. Exiting application."); Environment.Exit(1); } string userrole = rdr["UserRole"].ToString(); string status = rdr["Status"].ToString(); return Tuple.Create(docentry, userid, userrole, Convert.ToBoolean(status)); } else { MessageBox.Show("No user data found. Exiting application."); Environment.Exit(1); } } } catch (Exception ex) { MessageBox.Show("An error occurred: " + ex.Message); Environment.Exit(1); } return Tuple.Create(string.Empty, string.Empty, string.Empty, false); }Changes Made:
docentryis valid and handle cases where user data isn’t found.Environment.Exit(1)for a clean exit on errors.Ramco RamcoRank #462 · 5.4K reputationPosted Aug 21, 2024, 9:42 AM EDT
Hi
I am using .net web application. Will the below line will work
Environment.Exit(1);
Thanks