Solved

Retrieving CheckBoxList values in C#

Posted on 2010-09-09
16
560 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
 
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
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 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

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Runtime Error 2 28
Run time Error 4 34
Ajax Panel Not Updating When Triggered By Timer 4 44
Close form "before" open 3 27
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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now