Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using property and foreach write a class

Posted on 2012-04-02
6
Medium Priority
?
307 Views
Last Modified: 2012-04-08
Hello, I have an asp.net 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);
            con.Open();
            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);
            else
                cmd.Parameters.AddWithValue("@ProbDesc", e.NewValues["ProbDesc"]);
            if (e.NewValues["ICD"] == null)
                cmd.Parameters.AddWithValue("@ICD", DBNull.Value);
            else
                cmd.Parameters.AddWithValue("@ICD", e.NewValues["ICD"]);
            if (e.NewValues["CPT"] == null)
                cmd.Parameters.AddWithValue("@CPT", DBNull.Value);
            else
                cmd.Parameters.AddWithValue("@CPT", e.NewValues["CPT"]);
                      cmd.ExecuteNonQuery();
            con.Close();
        }

Open in new window

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

Regards,
chinmay.
0
 

Author Comment

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

Open in new window

And
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.
0
 
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.
0
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

by:zhshqzyc
ID: 37802143
Self learning.
0
 

Accepted Solution

by:
zhshqzyc earned 0 total points
ID: 37803531
0
 

Author Closing Comment

by:zhshqzyc
ID: 37820906
SELF RESLOVED IT.
0

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) 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.…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Loops Section Overview
Suggested Courses

971 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