Using property and foreach write a class

Posted on 2012-04-02
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.
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Author Comment

ID: 37802143
Self learning.

Accepted Solution

zhshqzyc earned 0 total points
ID: 37803531

Author Closing Comment

ID: 37820906

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to set focus on a dynamic control 18 47
.Net remove carriage returns, line feeds and tabbs 5 33
Data Analysis 7 58
Difference between Highcharts and Mapbox 10 44
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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