Solved

Retrieving CheckBoxList values in C#

Posted on 2010-09-09
16
563 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
  • 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

856 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