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 am starting a web application project that will have an Adobe Flex front-end
Is there any way to see view code converted to html other than starting
I am starting a project that will have many files (like a web servers
Starting a new rails project and we have a well-thought-out color palette, and want
I'm starting a .NET project and my developers have their own laptops. How can
I am starting a new project for a client today. I have done some
Im just starting with localization today and need some information. I have a project
i have a question about the theory here, i'm just starting a project which
I am a freelance web-developer, just starting out. In recent past, my project works
I have just downloaded jQuery 1.7 for a brand new project I'm starting. After

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.