Solved

Using property and foreach write a class

Posted on 2012-04-02
6
300 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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