0

I am trying to get this method to read values from the database as well as to get values from an asp.net webform.

I have tried the code below but never worked

    public void CheckLikedEvents()
    {
        if (User.Identity.IsAuthenticated)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myconn"].ConnectionString);
            try
            {
                Session["id"] = Context.User.Identity.GetUserId();
                SqlCommand cmd = new SqlCommand("Select Distinct SavedEvents.UserId, SavedEvents.EventId FROM SavedEvents WHERE UserId=@id and EventId =@eventId ", conn);
                SqlParameter param = new SqlParameter();
                param.ParameterName = "@id";
                param.Value = Session["id"];
                SqlParameter param1 = new SqlParameter();
                param1.ParameterName = "@eventId";
                param1.Value = Request.QueryString["EventId"];
                cmd.Parameters.Add(param);
                cmd.Parameters.Add(param1);

                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    Button lnkbtnGoing = dlEvents.FindControl("lnkbtnGoing") as Button;
                    lnkbtnGoing.Visible = false;
                }
                else
                {
                    Button lnkbtnGoing = dlEvents.FindControl("lnkbtnGoing") as Button;
                    lnkbtnGoing.Visible = true;
                }
            }
            catch (Exception ex)
            {

                lblErrormsg.Text = DatabaseErrorMessage(ex.Message);
            }
        }

    }

one of the errors I have got

A database error has occurred. Object reference not set to an instance of an object.

  • *where* did the error occur? was it perhaps on the `lnkbtnGoing.Visible` assignment, meaning that the problem is the control lookup? – Marc Gravell Oct 19 '19 at 07:58
  • Yes, the problem is with the control lookup, the button inside a data list and in order to access it, I have to use "dlname.FindControl()" as i understand. is there any other ways but to use ".findControl()"? – Mina Bebawi Oct 19 '19 at 20:06
  • key point: if the problem is with the control, only one (well, two) line of the code here is relevant, and it has *nothing* to do with sqlcommand, parameters, etc... – Marc Gravell Oct 19 '19 at 20:29

0 Answers0