Ramco Ramco

Ramco Ramco

  • 435
  • 3.4k
  • 547.4k

Error Message not getting displayed

Aug 21 2024 12:01 PM

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

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<String,String, String, Boolean> 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


Answers (3)