Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Using property and foreach write a class

Posted on 2012-04-02
Medium Priority
Last Modified: 2012-04-08
Hello, I have an gridview update method for updating a table. It works well. But I felt the code structure is not optimal and kind of ugly.
Could you please help me to reconsider the code? Maybe a tiered structure is better. Thanks.
string strCon =DataAccess.conn;

Open in new window

public class DataAccess
        public static string conn = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;

Open in new window

This part to be rewritten:
protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e)
            SqlConnection con = new SqlConnection(strCon);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText ="UPDATE Problem SET ProbDesc = @ProbDesc, ICD = @ICD, CPT = @CPT WHERE (ProbId = @ProbId)";
            if (e.NewValues["ProbDesc"] == null)
                cmd.Parameters.AddWithValue("@ProbDesc", DBNull.Value);
                cmd.Parameters.AddWithValue("@ProbDesc", e.NewValues["ProbDesc"]);
            if (e.NewValues["ICD"] == null)
                cmd.Parameters.AddWithValue("@ICD", DBNull.Value);
                cmd.Parameters.AddWithValue("@ICD", e.NewValues["ICD"]);
            if (e.NewValues["CPT"] == null)
                cmd.Parameters.AddWithValue("@CPT", DBNull.Value);
                cmd.Parameters.AddWithValue("@CPT", e.NewValues["CPT"]);

Open in new window

There are a lot of parameters are omitted, can we use property and foreach something like?
Question by:zhshqzyc
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
  • 4
  • 2
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37798335
Hi zhshqzyc,

Could you please tell me the purpose of this project?
Based on that I will be able to suggest and ORM or maybe simpler Entity Framework.


Author Comment

ID: 37801792
I mean some kind of
 public class cParameters
        public string paramName { get; set; }
        public string paramValue { get; set; }

Open in new window

public static void ExecuteNonQuery(string spName, List<cParameters> oParams)

Open in new window

foreach (cParameters oParam in oParams)
                                cmd.Parameters.AddWithValue(oParam.paramName, oParam.paramValue == null ? "" : oParam.paramValue.Replace("'", "''"));

Open in new window

But I just have no idea to put them together.
LVL 27

Expert Comment

by:Chinmay Patel
ID: 37802093
I meant to ask whether this is a commercial solution you are building or you are learning or making it for personal use?
Based on that I can suggest few alternatives.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 37802143
Self learning.

Accepted Solution

zhshqzyc earned 0 total points
ID: 37803531

Author Closing Comment

ID: 37820906

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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