Avatar of Brian
Brian
Flag for United States of America asked on

Retrieving CheckBoxList values in C#

Hello, I have the following code below that retireves values from my database that I would like to bind to the CheckBoxList control. However, I have a red line under the word "Information" that I can't seem to figure out. Please keep in mind that someone else gave me this code to use which is why i'm having issues.


                    // Check for CheckBox value if empty then the CheckBox is empty
                    foreach (DataRow Row in dtStudent.Rows)
                    {
                        if (!Information.IsDBNull(Row["testid"]))    <------ Message on this line.,
                        {
                            cblTests.Items.FindByValue(Row["testid"].ToString()).Selected = true;
                        }
                    }
ASP.NETC#.NET Programming

Avatar of undefined
Last Comment
Brian

8/22/2022 - Mon
Carl Tawn

Well, "Information" in your code sample must refer to either a static class, or some IDataReader derived object. The red line means it isn't recognised, which will either mean you're missing a reference/namespace or the object being refered to is out of scope.
kaufmed

Additionally, just about any time you receive a squiggly in VS (green or red), you can mouse over the offending text and you should receive a popup describing what VS is having an issue with.
Brian

ASKER
Well not sure if this helps but I tried to convert the VB code that I used before to C# and that may be where the problem is, but I don' know how to fix it :(.

VB Code:

                    ' Check for CheckBox value if empty then the CheckBox is empty
                    For Each Row As DataRow In dtStudent.Rows
                        If Not IsDBNull(Row("testid")) Then
                            TestList.Items.FindByValue(Row("testid").ToString()).Selected = True
                        End If
                    Next


C# code that was convert from above.

                    foreach (DataRow Row in dtStudent.Rows)
                    {
                        if (!Information.IsDBNull(Row["testid"]))
                        {
                            cblTests.Items.FindByValue(Row["testid"].ToString()).Selected = true;
                        }
                    }
Your help has saved me hundreds of hours of internet surfing.
fblack61
Mehdi Javan

You must use System.Convert.IsDBNull instead of Information.IsDBNull. Information class is in Microsoft.VisualBasic assembly which is not referenced in C# projects by default.
Carl Tawn

Try:

if (System.Convert.IsDBNull(Row["testid"]))
{
   // code here
}

Open in new window

Red-LineNetworks

Hi,

Information lives in the VB NameSpace. Try the below or a variant of it this.

if (!(IsDbNull(ds.Tables[1].Rows[i]["testid"]))
  {
      cblTests.Items.FindByValue(Row["testid"].ToString()).Selected = true;
  }

Hope this helps.

Thanks,
Red-LineNetworks
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ghayasurrehman

use the simplest one
if (ds.Tables[1].Rows[i]["testid"] != null)
  {
      cblTests.Items.FindByValue(Row["testid"].ToString()).Selected = true;
  }
kaufmed

@ghayasurrehman

DBNull is not the same as null. Your example is flawed.
Brian

ASKER
hi ghayasurrehman,

When i add your code i'm told that the name "ds" and "i" are not in the current context.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Mehdi Javan

Just use System.Convert.IsDBNull instead of Information.IsDBNull in your first code.
Brian

ASKER
Hello everyone,

for some reason when I run my code the values are not getting populated to my CheckBoxList control. Please see what I currently have. I need the code mentioned above to bind the values from the DB to the CheckBoxList control.

                    foreach (DataRow Row in dtStudent.Rows)
                    {
                        if (System.Convert.IsDBNull(Row["testid"]))
                        {
                            cblTests.Items.FindByValue(Row["testid"].ToString()).Selected = true;
                        }
                    }
Brian

ASKER
Hi ghayasurrehman,

Your solution in post 33640896 did not work.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Brian

ASKER
Hi mehdi_javan,

This did not work either.
kaufmed

>>  Hi ghayasurrehman,

>>  Your solution in post 33640896 did not work.

Of course it did not. Read post #33641588.

Perhaps you can try:
foreach (DataRow Row in dtStudent.Rows)
{
    if (!(Row.IsNull("testid"))
    {
        cblTests.Items.FindByValue(Row["testid"].ToString()).Selected = true;
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
kaufmed

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Brian

ASKER
Thank you. That worked out GREAT!!!!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy