We help IT Professionals succeed at work.

Retrieving CheckBoxList values in C#

asp_net2
asp_net2 asked
on
591 Views
Last Modified: 2012-05-10
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;
                        }
                    }
Comment
Watch Question

Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
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.

Author

Commented:
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;
                        }
                    }
Mehdi JavanSoftware Developer

Commented:
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 TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
Try:

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

Open in new window

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
use the simplest one
if (ds.Tables[1].Rows[i]["testid"] != null)
  {
      cblTests.Items.FindByValue(Row["testid"].ToString()).Selected = true;
  }
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
@ghayasurrehman

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

Author

Commented:
hi ghayasurrehman,

When i add your code i'm told that the name "ds" and "i" are not in the current context.
Mehdi JavanSoftware Developer

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

Author

Commented:
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;
                        }
                    }

Author

Commented:
Hi ghayasurrehman,

Your solution in post 33640896 did not work.

Author

Commented:
Hi mehdi_javan,

This did not work either.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
>>  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

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you. That worked out GREAT!!!!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.