Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7779239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:36:02+00:00 2026-06-01T18:36:02+00:00

I have a ugly code that can’t be reused. I have many similar queries.

  • 0

I have a ugly code that can’t be reused. I have many similar queries. I want to rewrite it with SqlParameterCollectionExtensions or other better ways. But I don’t know about SqlParameterCollectionExtensions at all.

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_DE SET ProbDesc = @ProbDesc, field_1 = @field_1, field_2 = @field_2, field_3 = @field_3, field_4 = @field_4, field_5 = @field_5, field_6 = @field_6, field_7 = @field_7 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["field_1"] == null)
            cmd.Parameters.AddWithValue("@field_1", DBNull.Value);
        else
            cmd.Parameters.AddWithValue("@field_1", e.NewValues["field_1"]);
        if (e.NewValues["field_2"] == null)
            cmd.Parameters.AddWithValue("@field_2", DBNull.Value);
        else
            cmd.Parameters.AddWithValue("@field_2", e.NewValues["field_2"]);
        if (e.NewValues["field_3"] == null)
            cmd.Parameters.AddWithValue("@field_3", DBNull.Value);
        else
            cmd.Parameters.AddWithValue("@field_3", e.NewValues["field_3"]);

        if (e.NewValues["field_4"] == null)
            cmd.Parameters.AddWithValue("@field_4", DBNull.Value);
        else
            cmd.Parameters.AddWithValue("@field_4", e.NewValues["field_4"]);
         \\ blah blah
        cmd.ExecuteNonQuery();
        con.Close();
     }

The sql parameters come from e or textbox etc.
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:36:03+00:00Added an answer on June 1, 2026 at 6:36 pm

    Maybe something like this? I assume the problem is that you have a variable number of values, depending on the problem table?

    private void UpdateProblem(string problemName, string problemDescription, int problemId, object[] fieldValues)
    {
        SqlConnection   con = null;
        SqlCommand      cmd = new SqlCommand();
        StringBuilder   sql = new StringBuilder();
        int             fieldCounter = 1;
    
        // start building the sql statement
        sql.AppendFormat("UPDATE {0} SET ProbDesc = @ProbDesc", problemName);
    
        // add the 'description' parameter
        cmd.Parameters.Add(new SqlParameter("@ProbDesc", problemDescription));
    
        // add each field value to the update statement... the SqlParameter will infer the database type.
        foreach(object fieldValue in fieldValues)
        {
            // add additional SET clauses to the statement
            sql.AppendFormat(",field{0} = @field{0}", fieldCounter);
    
            // add the field parameter to the command's collection
            cmd.Parameters.Add(new SqlParameter(String.Format("@field{0}", fieldCounter), fieldValue));
    
            fieldCounter++;
        }
    
        // finish up the SQL statement by adding the where clause
        sql.Append(" WHERE (ProbId = @ProbId)");
    
        // add the 'problem ID' parameter to the command's collection
        cmd.Parameters.Add(new SqlParameter("@ProbId", problemId));
    
        // finally, execute the SQL
        try
        {
            con.Open();
    
            cmd.Connection = con;
    
            cmd.CommandText = sql.ToString();
    
            cmd.ExecuteNonQuery();
        }
        catch(SqlException ex)
        {
            // do some exception handling
        }
        finally
        {
            if(con != null)
                con.Dispose();
        }
    }
    

    An example to call this code would be:

    public void UpdateProblemDe()
    {
        int         problemId = FetchCurrentProblemId();
        string      field1 = e.NewValues["field_1"];
        string      field2 = e.NewValues["field_2"];
        string      field3 = ddlField3.SelectedValue;
        int         field4 = Convert.ToInt32(e.NewValues["field_4"]);
        string      field5 = txtField5.Text;
        DateTime    field6 = DateTime.Now.AddSeconds(Convert.ToInt32(ddlField6.SelectedValue));
        string      field7 = txtField7.Text;
        object[6]   fieldValues;
    
        if(field1 != null)
            fieldValues[0] = field1;
        else
            fieldValues[0] = DBNull.Value;
    
        if(field2 != null)
            fieldValues[1] = field2;
        else
            fieldValues[1] = DBNull.Value
    
        fieldValues[2] = field3;
        fieldValues[3] = field4;    
        fieldValues[4] = field5;
        fieldValues[5] = field6;
        fieldValues[6] = field7;
    
        UpdateProblem("Problem_DE", "Houston, we have a problem", problemId, fieldValues);  
    }   
    

    The example code above is obviously just a demonstration of how to make an array of objects stored from page control values and doesn’t include any data validation that you will need to implement in production code.

    If you won’t know how big the object[] array needs to be at runtime, you can change it to be a generic List of objects and add the items dynamically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ugly piece of code that adds event handlers. The problem is,
I have the following ugly if statement that is part of a class that
So I have made a little piece of code that will insert and manipulate
We need to have some kind of app that we can deploy to an
I have a few blocks of code, inside a function of some object, that
I have some C# code that walks XML schemata using the Xml.Schema classes from
I have a 'widget' that comprises an html/css block of code. It is a
I have some Objective-C code that does the job for me. But is it
I have made a wordpress plugin that can fetch an unknown number of Google
Let's say I have some isolated Python code that processes data produced by some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.