Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET C# Form Submit | Can my function be shorter and faster?
    primarykey
    data
    text
    <p>I am starting a project and I have an <code>Onclick</code> 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?</p> <pre><code>protected void PageSubmit_Click(object sender, EventArgs e) { string storedProc = ""; string successMsg = ""; DataTable dt = new DataTable(); if (hfPageID.Value.Length &gt; 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); } </code></pre> <p>Also, I set up a check in the stored procedure to fail the <code>pageUpdate</code> 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?</p> <pre><code>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 ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload