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;
                        }
                    }
LVL 4
asp_net2Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
Missed a paren:
foreach (DataRow Row in dtStudent.Rows)
{
    if (!(Row.IsNull("testid")))
    {
        cblTests.Items.FindByValue(Row["testid"].ToString()).Selected = true;
    }
}

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
 
käµfm³d 👽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.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
asp_net2Author 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;
                        }
                    }
0
 
mehdi_javanCommented:
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.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Try:

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

Open in new window

0
 
Red-LineNetworksCommented:
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
0
 
ghayasurrehmanCommented:
use the simplest one
if (ds.Tables[1].Rows[i]["testid"] != null)
  {
      cblTests.Items.FindByValue(Row["testid"].ToString()).Selected = true;
  }
0
 
käµfm³d 👽Commented:
@ghayasurrehman

DBNull is not the same as null. Your example is flawed.
0
 
asp_net2Author Commented:
hi ghayasurrehman,

When i add your code i'm told that the name "ds" and "i" are not in the current context.
0
 
mehdi_javanCommented:
Just use System.Convert.IsDBNull instead of Information.IsDBNull in your first code.
0
 
asp_net2Author 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;
                        }
                    }
0
 
asp_net2Author Commented:
Hi ghayasurrehman,

Your solution in post 33640896 did not work.
0
 
asp_net2Author Commented:
Hi mehdi_javan,

This did not work either.
0
 
käµfm³d 👽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

0
 
asp_net2Author Commented:
Thank you. That worked out GREAT!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.