Solved

Retrieving CheckBoxList values in C#

Posted on 2010-09-09
16
566 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;
                        }
                    }
0
Comment
Question by:asp_net2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
  • +3
16 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33640472
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33640510
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
 
LVL 4

Author Comment

by:asp_net2
ID: 33640558
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Expert Comment

by:mehdi_javan
ID: 33640655
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 33640682
Try:

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

Open in new window

0
 
LVL 1

Expert Comment

by:Red-LineNetworks
ID: 33640855
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
 
LVL 3

Expert Comment

by:ghayasurrehman
ID: 33640896
use the simplest one
if (ds.Tables[1].Rows[i]["testid"] != null)
  {
      cblTests.Items.FindByValue(Row["testid"].ToString()).Selected = true;
  }
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33641588
@ghayasurrehman

DBNull is not the same as null. Your example is flawed.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 33642327
hi ghayasurrehman,

When i add your code i'm told that the name "ds" and "i" are not in the current context.
0
 
LVL 3

Expert Comment

by:mehdi_javan
ID: 33642352
Just use System.Convert.IsDBNull instead of Information.IsDBNull in your first code.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 33645795
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
 
LVL 4

Author Comment

by:asp_net2
ID: 33645931
Hi ghayasurrehman,

Your solution in post 33640896 did not work.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 33645934
Hi mehdi_javan,

This did not work either.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33646537
>>  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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 33646545
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
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 33646566
Thank you. That worked out GREAT!!!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question