Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 988855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:41:51+00:00 2026-05-16T05:41:51+00:00

First off, I am new to programming (especially with C#) and thanks for your

  • 0

First off, I am new to programming (especially with C#) and thanks for your help.

I have a static web form with about 150 form objects (most checkboxes). I decided to go 1 record per form submission in the sql db. So, for example, question X has a choice of 5 checkboxes. Each of these 5 checkboxes has a column in the db.

I have the post page complete(working) and am building an edit page where I load the record and then populate the form.

How I am doing this is by passing a stored proc the id and then putting all the returned column values into the according object properties, then setting the asp control object to them.

An example of setting the asp controls to the selected value:

questionX.Items[0].Selected = selectedForm.questionX0
questionX.Items[1].Selected = selectedForm.questionX1
questionX.Items[2].Selected = selectedForm.questionX2

As you see, this is very tiresome since there are over 150 of these to do. Also, I just found out if the response is NULL then I get the error that it cant be converted to a string. So, I have added this line of code to get past it:

This is the part where I am populating the returned column values into the object properties (entity is the object):

if (!String.IsNullOrEmpty((string)reader["questionX0"].ToString()))
{entity.patientUnderMdTreatment = (string)reader["questionX0"];}

So, instead of having to add this if then statement 150+ times. There must be a way to do this more efficiently.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:41:52+00:00Added an answer on May 16, 2026 at 5:41 am

    First of all, it seems that you are using string.IsNullOrEmpty(value), but this won’t check for the special DBNull value that is returned from databases when the data is null. You should use something more akin to value is DBNull.

    The rest of your problem sounds complex, so please don’t be put off if my answer is complex too. Personally I would use custom attributes:

    Declare a custom attribute

    The following is a skeleton to give you the idea. You may want to use the “Attribute” code snippet in Visual Studio to find out more about how to declare these.

    [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
    public sealed class QuestionColumnAttribute : Attribute
    {
        public string ColumnName { get; private set; }
        public QuestionColumnAttribute(string columnName)
        {
            ColumnName = columnName;
        }
    }
    

    Use the custom attribute in the entity class

    Where you declare your entity class, add this custom attribute to every field, for example where patientUnderMdTreatment is declared:

    [QuestionColumn("questionX0")]
    public string patientUnderMdTreatment;
    

    Iterate over the fields

    Instead of iterating over the columns in the reader, iterate over the fields. For each field that has a QuestionColumnAttribute on it, get the relevant column from the reader:

    foreach (var field in entity.GetType().GetFields())
    {
        var attributes = field.GetCustomAttributes(typeof(QuestionColumnAttribute), true);
        if (attributes.Length == 0)
            continue;
    
        object value = reader[attributes[0].ColumnName];
        if (!(value is DBNull))
            field.SetValue(entity, value.ToString());
    }
    

    For the first part of your question where you set the ASP controls, you can use a similar strategy iterating over the fields of selectedForm, and this is probably simpler because you don’t need a custom attribute — just take only the fields whose name starts with “questionX”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, a lil about me, i'm very new to GUI programming, especially with
First off, i'm fairly new to programming, I've built a few asmx web services
First off I am very new to Objective C and iPhone programming. Now that
First off, I'm new to Java, so please be gentle. I have a JFrame
First off I'm new at programming. I'm creating an app with one navigation bar
First off I am brand new to C and have been thrown into a
Okay so first off, Im pretty new to programming, Ive only read a bit
First off i'm new to Linux programming so apologies if this makes no sense
First off I am completely new to Javascript but I have some HTML/CSS experience.
First off I am still new to objective-c and still trying to learn as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.