-2

I'm trying the show some data from my table by clicking on a link label, but when i launch the application, i have some error like : Object does not contain a definition of tag and no accessible extension methode error

Error line : int appID = (LinkLabel)sender.Tag;

Code you please help me to fix this error or if you have other idea to get the ID

Here is My code :

   private void ShowAppointmentDetail(object sender, EventArgs e)
    {
        int appID = (LinkLabel)sender.Tag;

        SqlCommand sql = new SqlCommand("select * from RDV whereID_RDV = {appID}");

        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(sql);
        sda.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            DataRow row = dt.Rows[0];
            {
                frmManageAppointment withBlock = new frmManageAppointment();
                withBlock.Nom.Text = row["Nom"].ToString();
                withBlock.Prénom.Text = row["Prénom"].ToString();
                withBlock.NUM.Text = row["Num"].ToString();
                withBlock.date.Text = row["DateRDV"].ToString();
                withBlock.ShowDialog();
            }
            DisplayCurrentDate();
        }
    }

Thank you

kamal
  • 9
  • 5
  • 1
    first thing is that the `Tag` property is an `object` and you can't get it directly into an `int`, it needs `(int)` cast or `int.Parse` etc. Second thing is that the event you are using is not for clicking on `linklabel` so `sender` does not contain any of `linklabel` attributes. you can make a variable to carry your `appID`. why are you using `Tag`?! – iѕєρєня Sep 05 '21 at 21:21
  • 1
    _var t = (sender as LinkLabel).Tag;_ then you can convert the Tag value (an object) to an integer if it is not null – Steve Sep 05 '21 at 21:23
  • First line should read (C#8 pattern matching syntax): `if (sender is not LinkLabel lili || lili.Tag is not int appID) return;` – lidqy Sep 05 '21 at 22:14
  • `int appID = (LinkLabel)sender.Tag;` -> `int appID = (int)((LinkLabel)sender).Tag;` – mjwills Sep 06 '21 at 03:51
  • Thank you for your feedback - @Steve yeah that's correct, it solves my issue ... thank you so much – kamal Sep 10 '21 at 18:51

1 Answers1

0
     private void ShowAppointmentDetail(object sender, EventArgs e)
        {
        Was :  int appID = (LinkLabel)sender.Tag;
        Answer :  var appID = (sender as LinkLabel).Tag;


    SqlCommand sql = new SqlCommand("select * from RDV whereID_RDV = {appID}");

    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter(sql);
    sda.Fill(dt);

    if (dt.Rows.Count > 0)
    {
        DataRow row = dt.Rows[0];
        {
            frmManageAppointment withBlock = new frmManageAppointment();
            withBlock.Nom.Text = row["Nom"].ToString();
            withBlock.Prénom.Text = row["Prénom"].ToString();
            withBlock.NUM.Text = row["Num"].ToString();
            withBlock.date.Text = row["DateRDV"].ToString();
            withBlock.ShowDialog();
        }
        DisplayCurrentDate();
    }
}
kamal
  • 9
  • 5