Solved

Using property and foreach write a class

Posted on 2012-04-02
6
297 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now