locked
Redirect command RRS feed

  • Question

  • User1357167467 posted

    Hi, I've got a form which accepts data entry and puts it into a SQL table, one of the hidden fields takes a GUID.

    I also want the submit action to run a redirect to another page which in turn displays the data entered on the previous form, using the GUID as an identifier in a querystring.

    Problem is I cannot get it to include the GUID in the querystring.

    Abrreviated code below. All suggestions most welcome.

    private void ExecuteInsert(string string JobType, string JobTitle, string tempadid, string submitted )
            {
                SqlConnection conn = new SqlConnection(GetConnectionString());
                string sql = "INSERT INTO ADVERTS([JobType], [JobTitle] , [TempAdId], [Submitted]) Values (@JobType, @JobTitle, @tempadid, @submitted )";
    
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    SqlParameter[] param = new SqlParameter[4];
                    param[0] = new SqlParameter("@JobType", SqlDbType.VarChar, 50);
                    param[1] = new SqlParameter("@JobTitle", SqlDbType.VarChar, 50);
                    param[2] = new SqlParameter("@tempadid", SqlDbType.VarChar, 50);
                    param[3] = new SqlParameter("@submitted", SqlDbType.VarChar, 50);
    
                    param[0].Value = JobType;
                    param[1].Value = JobTitle;
                    param[2].Value = Guid.NewGuid().ToString();
                    param[3].Value = DateTime.Now;
    
    
                    for (int i = 0; i < param.Length; i++)
                    {
                        cmd.Parameters.Add(param[i]);
                    }
    
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    string msg = "Insert Error:";
                    msg += ex.Message;
                    throw new Exception(msg);
                }
                finally
                {
                    Response.Redirect("preview.aspx?strTAD="+@TempADId);
                }
            }
    
            protected void ButtonAdvertSub_Click(object sender, EventArgs e)
            {
                //call the method to execute insert to the database
                ExecuteInsert(JobType.Text,
                                JobTitle.Text,
                                TempADId.Text,
                                Submitted.Text
                                  );
            }
    
        }
    }

    All it's giving me is domain.com/preview?strTAD= with no value for strTAD. The preview page works fine if I enter a valid GUID at the end of the given url, problem has to be in the above code, but I cannot work out what.

    Thanks, Phil

    Wednesday, November 13, 2019 8:12 PM

Answers

  • User753101303 posted

    It passes its value to your stored procedure. It's entirely unrelated to the @TempADId variable found later in your code. See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim

    If it compiles fine it seems you declared a TempADId variable somewhere which is not the same than your tempadid parameter and which seems to never be assigned.

    The code I suggested earlier creates a guid value, pass this value to your SP and you can then use this same guid value later in your code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 14, 2019 9:14 PM

All replies

  • User753101303 posted

    Hi,

    For now there is nothing showing how TempADId is populated. Seems a possible confusion with tempadid C# being case sensitive. You have other stuff which shouldn't even compile such as string string JobType

    I suspect your intent is to have perhaps something such as :

    private void ExecuteInsert(string JobType, string JobTitle, string tempadid, string submitted)
    {
    // Do you really pass a value for tempadid value ???
    ...
                    tempadid = Guid.NewGuid().ToString();
                    param[2].Value = tempadid;
    ...
                finally
                {
                    Response.Redirect("preview.aspx?strTAD="+tempadid);
                }
    

    Wednesday, November 13, 2019 8:38 PM
  • User-1780421697 posted

    First of all you need to make sure that TempADId.Text have some value, I think you need to use [TempADId.Value] not [TempADId.Text] property here. so that you can get value of asp hidden field.

    Thursday, November 14, 2019 9:21 AM
  • User1357167467 posted

    The TempADId gets it's value from 

    param[2].Value = Guid.NewGuid().ToString();

    This is getting written to the SQL table, so I believe the GUID is being created and inserted properly. I'll try with .value and see where that goes.

    Thursday, November 14, 2019 7:37 PM
  • User1357167467 posted

    It could be my abbreviating which makes it look odd, but the code all works if all I wanted it to do was send the form values to SQL.

    But the second act of the button's performance is to open a page (preview.aspx) with a querystring value of the GUID, which in turn is used by a SQL query to populate a page with that line from the SQL table.

    Thursday, November 14, 2019 7:39 PM
  • User753101303 posted

    It passes its value to your stored procedure. It's entirely unrelated to the @TempADId variable found later in your code. See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim

    If it compiles fine it seems you declared a TempADId variable somewhere which is not the same than your tempadid parameter and which seems to never be assigned.

    The code I suggested earlier creates a guid value, pass this value to your SP and you can then use this same guid value later in your code.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 14, 2019 9:14 PM
  • User-1780421697 posted
    private void ExecuteInsert(string string JobType, string JobTitle, string tempadid, string submitted )
            {
    var temp = Guid.NewGuid().ToString(); SqlConnection conn = new SqlConnection(GetConnectionString()); string sql = "INSERT INTO ADVERTS([JobType], [JobTitle] , [TempAdId], [Submitted]) Values (@JobType, @JobTitle, @tempadid, @submitted )"; try { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); SqlParameter[] param = new SqlParameter[4]; param[0] = new SqlParameter("@JobType", SqlDbType.VarChar, 50); param[1] = new SqlParameter("@JobTitle", SqlDbType.VarChar, 50); param[2] = new SqlParameter("@tempadid", SqlDbType.VarChar, 50); param[3] = new SqlParameter("@submitted", SqlDbType.VarChar, 50); param[0].Value = JobType; param[1].Value = JobTitle; param[2].Value = temp; param[3].Value = DateTime.Now; for (int i = 0; i < param.Length; i++) { cmd.Parameters.Add(param[i]); } cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert Error:"; msg += ex.Message; throw new Exception(msg); } finally { Response.Redirect("preview.aspx?strTAD="+temp); } } protected void ButtonAdvertSub_Click(object sender, EventArgs e) { //call the method to execute insert to the database ExecuteInsert(JobType.Text, JobTitle.Text, TempADId.Text, Submitted.Text ); } } }

    Friday, November 15, 2019 4:10 AM