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 4053724
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:29:01+00:00 2026-05-20T14:29:01+00:00

I am starting a project and I have an Onclick function running in the

  • 0

I am starting a project and I have an Onclick function running in the code behind of my web form. I was curious if there was a way to make the code shorter and/or faster?

protected void PageSubmit_Click(object sender, EventArgs e)
{
    string storedProc = "";
    string successMsg = "";

    DataTable dt = new DataTable();
    if (hfPageID.Value.Length > 0)
    {
        storedProc = "pageUpdate";
        successMsg = "Page updated!";
    }
    else
    {
        storedProc = "pageInsert";
        successMsg = "Page inserted!";
    }
    using (SqlConnection con = new SqlConnection(Global.conString))
    {
        con.Open();
        using (SqlCommand cmd = new SqlCommand(storedProc, con))
        {
            if (storedProc == "pageUpdate")
            {
                cmd.Parameters.Add("@originalID", SqlDbType.VarChar).Value = hfPageID.Value;
            }
            cmd.Parameters.Add("@title", SqlDbType.VarChar).Value = Global.SafeSqlLiteral(txtPage.Text, 1);
            cmd.Parameters.Add("@contentTypeID", SqlDbType.VarChar).Value = rblContentTypesGetAll.SelectedValue;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.ExecuteNonQuery();
        }
        con.Close();

        //Update Content Page Repeater
        using (SqlCommand cmd = new SqlCommand("pageGetAll", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);
            }
        }
    }

    Session["formProcessed"] = successMsg;
    Response.Redirect(redirectURL);
}

Also, I set up a check in the stored procedure to fail the pageUpdate if a title conflicts with an existing title. How can I check if it actually updated when it’s coming back from the stored procedure?

ALTER PROCEDURE [dbo].[pageUpdate]
    @originalID uniqueidentifier,
    @contentTypeID varchar (100),
    @title varchar (100)
AS 

UPDATE 
    pages
SET
    contentTypeID = COALESCE (@contentTypeID, contentTypeID),
    title = COALESCE (@title, title)
WHERE
    ID = @originalID
    AND 
        NOT EXISTS
        (
            SELECT
                *
            FROM
                pages
            WHERE
                title = @title
        )
  • 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-05-20T14:29:02+00:00Added an answer on May 20, 2026 at 2:29 pm

    There’s a number of things you could do that would certainly help readability and code reuse.

    Seperate your page/UI logic from the database logic. i.e. have a class that handled data access and call that from your page rather than have it on the click event. If you wanted this data elsewhere, you’d have to copy and paste the code rather than just call the same method on a common object.

    Furthermore to cut down your database code, you could look at using a Data Access Application Block as featured in the Enterprise Library like these here. It’ll handle connections and exceptions for you amongst other benefits listed in the documentation.

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

Sidebar

Related Questions

I'm starting a new project and have decided to try to incorporate DDD patterns
I have a new project starting up, and I was planning to use git
I am starting a project for which we will have a thin client, sending
I'm starting a project which requires reading outlook msg files in c#. I have
I'm starting to build up Unit Tests for a project we have. We've decided
I am starting a small project with one other developer and need to setup
Has anyone done something like this? How? I'm just starting a project that will
There exist any open source cms rtl ltr with a front end to work
I'm still feeling my way around C++, and am a complete ATL newbie, so
What strategies can be used in general to decrease build times for any Xcode

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.