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 7582037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:20:27+00:00 2026-05-30T18:20:27+00:00

Hello and good day to all. I have an issue on coding, i have

  • 0

Hello and good day to all. I have an issue on coding, i have long line of codes just to show 16 items from sql server to 16 textblocks, there is no error but i want to maintain the short line of codes. Here is 2 textblocks(out of 16) example codes:

            orgDa.SelectCommand = conn.CreateCommand();
            orgDa.SelectCommand.CommandText = "select OrganizationName from Organizationtbl where OrgID=1";
            orgDa.SelectCommand.CommandType = CommandType.Text;
            orgDa.Fill(ds, "Organizationtbl");

            deptDa.SelectCommand = conn.CreateCommand();
            deptDa.SelectCommand.CommandText = "select DepartmentName from Departmenttbl where DeptID=1";
            deptDa.SelectCommand.CommandType = CommandType.Text;
            deptDa.Fill(ds, "Departmenttbl");

            if (ds.Tables["Organizationtbl"].Rows.Count == 1)
            {
                foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
                {
                    if (orgItem.IsNull("OrganizationName"))
                    {
                        foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
                        {
                            textblock_EventTitle0.Text = deptItem["DepartmentName"].ToString();
                        }
                    }
                    else
                    {
                        textblock_EventTitle0.Text = orgItem["OrganizationName"].ToString();
                    }
                }
            }
            else
            {
                foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
                {
                    if (deptItem.IsNull("DepartmentName"))
                    {
                        foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
                        {
                            textblock_EventTitle0.Text = orgItem["OrganizationName"].ToString();
                        }
                    }
                    else
                    {
                        textblock_EventTitle0.Text = deptItem["DepartmentName"].ToString();
                    }
                }
            }

            orgDa.SelectCommand = conn.CreateCommand();
            orgDa.SelectCommand.CommandText = "select OrganizationName from Organizationtbl where OrgID=2";
            orgDa.SelectCommand.CommandType = CommandType.Text;
            orgDa.Fill(ds, "Organizationtbl");

            deptDa.SelectCommand = conn.CreateCommand();
            deptDa.SelectCommand.CommandText = "select DepartmentName from Departmenttbl where DeptID=2";
            deptDa.SelectCommand.CommandType = CommandType.Text;
            deptDa.Fill(ds, "Departmenttbl");

            if (ds.Tables["Organizationtbl"].Rows.Count == 1)
            {
                foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
                {
                    if (orgItem.IsNull("OrganizationName"))
                    {
                        foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
                        {
                            textblock_EventTitle1.Text = deptItem["DepartmentName"].ToString();
                        }
                    }
                    else
                    {
                        textblock_EventTitle1.Text = orgItem["OrganizationName"].ToString();
                    }
                }
            }
            else
            {
                foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
                {
                    if (deptItem.IsNull("DepartmentName"))
                    {
                        foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
                        {
                            textblock_EventTitle1.Text = orgItem["OrganizationName"].ToString();
                        }
                    }
                    else
                    {
                        textblock_EventTitle1.Text = deptItem["DepartmentName"].ToString();
                    }
                }
            }

How can i show 16 items from sql to 16 textblocks in just one loop? i need your help. Thank you in advance.

  • 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-30T18:20:28+00:00Added an answer on May 30, 2026 at 6:20 pm

    Generally, if you have code that is repeated, try to do the following:

    1. Take the code for one item, move it into a function.

    2. Find everything that differs for each item: like queries, table names, column names, controls etc.. Add a parameter to the method for each of them and replace each occurrence in the code with the parameter.

    3. Invoke the method one time for each item, passing in the right parameters.

    It’s basically a two step refactoring: first, extract method. Second, extract parameter.

    However, in your case, mainly the OrgId and DeptID seem to change (and the control to assign the value to), so read about parameterized queries, e.g. http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx

    Your extracted method could look like this (not tested, nor compiled):

        void LoadStuff(int id, TextBox control){
                orgDa.SelectCommand = conn.CreateCommand();
                orgDa.SelectCommand.CommandText = "select OrganizationName from Organizationtbl where OrgID=@orgId";
                orgDa.SelectCommand.CommandType = CommandType.Text;
                var parameter = orgDa.SelectCommand.CreateParameter();
                parameter.ParameterName = "@orgId";
                parameter.Value = id;
                orgDa.SelectCommand.AddParameter(parameter);
                orgDa.Fill(ds, "Organizationtbl");
    
                deptDa.SelectCommand = conn.CreateCommand();
                deptDa.SelectCommand.CommandText = "select DepartmentName from Departmenttbl where DeptID=@deptID";
                deptDa.SelectCommand.CommandType = CommandType.Text;
                parameter = orgDa.SelectCommand.CreateParameter();
                parameter.ParameterName = "@deptID";
                parameter.Value = id;
                orgDa.SelectCommand.AddParameter(parameter);
                deptDa.Fill(ds, "Departmenttbl");
    
                if (ds.Tables["Organizationtbl"].Rows.Count == 1)
                {
                    foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
                    {
                        if (orgItem.IsNull("OrganizationName"))
                        {
                            foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
                            {
                                control.Text = deptItem["DepartmentName"].ToString();
                            }
                        }
                        else
                        {
                            control.Text = orgItem["OrganizationName"].ToString();
                        }
                    }
                }
                else
                {
                    foreach (DataRow deptItem in ds.Tables["Departmenttbl"].Rows)
                    {
                        if (deptItem.IsNull("DepartmentName"))
                        {
                            foreach (DataRow orgItem in ds.Tables["Organizationtbl"].Rows)
                            {
                                control.Text = orgItem["OrganizationName"].ToString();
                            }
                        }
                        else
                        {
                            control.Text = deptItem["DepartmentName"].ToString();
                        }
                    }
                }
        }
    

    And then call it with:

    LoadStuff(1, textblock_EventTitle0);
    LoadStuff(2, textblock_EventTitle1);
    LoadStuff(3, textblock_EventTitle2);
    ....
    

    or with some smarter loop.

    This could ca be simplified even more, of course. There seems to be a lot of similarities in the two branches of the if-else construct.

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

Sidebar

Related Questions

Hello and good day to all. I have the following code to retrieve from
Hello and good day to all. I have a little question about like statement
Hello and good day to you. Situation: For some reason, from time to time
I have a string: hello good old world and i want to upper case
I want to represent the list hi, hello, goodbye, good day, howdy (with that
So I've spent all day looking for good examples of using xpath / xquery
Hello and good day to you. Is this true (is it required by standard)
Hello and good day to you. Following code fragment compiles on cl.exe (15.00.30729.01) and
Hello good Stack Overflow people, I do have a business problem and would like
I have a file abc.txt with contents like: hello hi good bad ... ....

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.