Click here to Skip to main content
       

ASP.NET

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionwebpage has resulted in too many redirectsmemberJassim Rahma21 Dec '12 - 9:46 
Hi,
 
Why I am getting this error when insert is successful?!!! it should redirect to ~/Channels!
 
here is my code:
 
try
{
    sql_connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["SQLdb"].ConnectionString);
    sql_connection.Open();
                
    if (RouteData.Values["GUID"].ToString() == "New") sql_command = new MySqlCommand("sp_add_new_channel", sql_connection);
    else sql_command = new MySqlCommand("sp_update_channel", sql_connection);
                
    sql_command.CommandType = CommandType.StoredProcedure;
 
    if (RouteData.Values["GUID"].ToString() == "New") sql_command.Parameters.AddWithValue("param_member_id", Convert.ToInt32(Session["member_id"])).MySqlDbType = MySqlDbType.Int32; 
    else sql_command.Parameters.AddWithValue("param_channel_guid", RouteData.Values["GUID"].ToString()).MySqlDbType = MySqlDbType.VarChar;                
 
    sql_command.Parameters.AddWithValue("param_channel_name", txtChannelName.Text.Trim()).MySqlDbType = MySqlDbType.VarChar;
    sql_command.Parameters.AddWithValue("param_channel_address", txtChannelAddress.Text.Trim()).MySqlDbType = MySqlDbType.VarChar;
    sql_command.Parameters.AddWithValue("param_channel_language", Convert.ToInt32(cboLanguage.Value)).MySqlDbType = MySqlDbType.Int32;
    sql_command.Parameters.AddWithValue("param_channel_description", txtChannelDescription.Text.Trim()).MySqlDbType = MySqlDbType.VarChar;
    sql_command.Parameters.AddWithValue("param_channel_allow_comment", chkAllowComment.Checked).MySqlDbType = MySqlDbType.Bit;
    sql_command.Parameters.AddWithValue("param_channel_allow_like", chkAllowLikeIt.Checked).MySqlDbType = MySqlDbType.Bit;
    sql_command.Parameters.AddWithValue("param_channel_allow_share", chkAllowShare.Checked).MySqlDbType = MySqlDbType.Bit;
    sql_command.Parameters.AddWithValue("param_show_profile_photo", chkShowProfilePhoto.Checked).MySqlDbType = MySqlDbType.Bit;
    if (RouteData.Values["GUID"].ToString() == "New") sql_command.Parameters.Add("param_channel_guid", MySqlDbType.VarChar).Direction = ParameterDirection.Output;
    sql_command.Parameters.Add("param_is_exist", MySqlDbType.Bit).Direction = ParameterDirection.Output;                
 
    // execute the query;
    int result_rows = sql_command.ExecuteNonQuery();
 
    if (Convert.ToBoolean(sql_command.Parameters["param_is_exist"].Value) != true)
    {
        // member_id = sql_command.Parameters["param_record_identity"].Value.ToString();
        // member_guid = sql_command.Parameters["param_record_guid"].Value.ToString();
        // activation_code = sql_command.Parameters["param_activation_code"].Value.ToString();

        website_event_log.add_event_log("Membership", txtChannelName.Text.Trim(), "You have successfully signed up");
 
        Response.Redirect("~/Channels", true);
    }
    else
    {
        Session["message_title"] = "Error";
        Session["message_title_Color"] = Color.Red.ToArgb();
        Session["message_text"] = "The same channel address already exists. Please try another address.";
        Session["message_button_title"] = "Back";
        Session["message_button_url"] = Request.UrlReferrer.ToString();
 
        Response.Redirect("~/Message", false);
    }
}
catch (Exception exp)
{
    Session["message_title"] = "ERROR..";
    Session["message_text"] = String.Format("Please try again.......{0}{1}", "", exp.Message);
    Session["message_button_title"] = "Back to Sign-in";
    Session["message_button_url"] = Request.UrlReferrer.ToString();
 
    Response.Redirect("~/Message", false);
}
finally
{
    // if (sql_reader != null) sql_reader.Close();
    if (sql_connection != null)
    {
        if (sql_connection.State == ConnectionState.Open)
            sql_connection.Close();
    }
}

 
Technology News @ www.JassimRahma.com

GeneralRe: webpage has resulted in too many redirects PinmemberJassim Rahma23 Dec '12 - 8:36 
but the finally will run no matter what try was successful or error was catched? right?

 
Technology News @ www.JassimRahma.com

GeneralRe: webpage has resulted in too many redirects Pinmemberjkirkerx23 Dec '12 - 8:47 
Yes that is correct.
 
So you make an exitCode as an integer = 2
 
exitCode integer = 2
 
do your checks,
 
in the catch, make exitCode = 1
 
in the Try, exitCode = 0
 
So in the finally, if the exitCode is 2, it's complete failure
if the exitCode is 1, you trapped the error, log it or display it
if the exitCode is 0, the program ran correctly
 
  exitCode integer = 2;
 
Try
  // Run your code
  exitCode = 0
 
catch ex as exception
  exitCode = 1
 
Finally
  switch exitCode {
     case 2
 
     case 1
 
     case 0
      response.redirect("~/correct");
    }
  }
}

GeneralRe: webpage has resulted in too many redirects PinmemberJassim Rahma24 Dec '12 - 9:10 
I removed the try..catch..finally block and ran the code.. everything worked fine!!!
 
so where is the problem?
 
here is the code after commenting the try..catch..finally:
 
// try
// {
    sql_connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["SQLdb"].ConnectionString);
    sql_connection.Open();
                
    if (RouteData.Values["GUID"].ToString() == "New") sql_command = new MySqlCommand("sp_add_new_channel", sql_connection);
    else sql_command = new MySqlCommand("sp_update_channel", sql_connection);
                
    sql_command.CommandType = CommandType.StoredProcedure;
 
    if (RouteData.Values["GUID"].ToString() == "New") sql_command.Parameters.AddWithValue("param_member_id", Convert.ToInt32(Session["member_id"])).MySqlDbType = MySqlDbType.Int32;
    else sql_command.Parameters.AddWithValue("param_channel_guid", RouteData.Values["GUID"].ToString()).MySqlDbType = MySqlDbType.VarChar;
 
    sql_command.Parameters.AddWithValue("param_channel_name", txtChannelName.Text.Trim()).MySqlDbType = MySqlDbType.VarChar;
    sql_command.Parameters.AddWithValue("param_channel_address", txtChannelAddress.Text.Trim()).MySqlDbType = MySqlDbType.VarChar;
    sql_command.Parameters.AddWithValue("param_channel_language", Convert.ToInt32(cboLanguage.Value)).MySqlDbType = MySqlDbType.Int32;
    sql_command.Parameters.AddWithValue("param_channel_description", txtChannelDescription.Text.Trim()).MySqlDbType = MySqlDbType.VarChar;
    sql_command.Parameters.AddWithValue("param_channel_allow_comment", chkAllowComment.Checked).MySqlDbType = MySqlDbType.Bit;
    sql_command.Parameters.AddWithValue("param_channel_allow_like", chkAllowLikeIt.Checked).MySqlDbType = MySqlDbType.Bit;
    sql_command.Parameters.AddWithValue("param_channel_allow_share", chkAllowShare.Checked).MySqlDbType = MySqlDbType.Bit;
    sql_command.Parameters.AddWithValue("param_show_profile_photo", chkShowProfilePhoto.Checked).MySqlDbType = MySqlDbType.Bit;
    if (RouteData.Values["GUID"].ToString() == "New") sql_command.Parameters.Add("param_channel_guid", MySqlDbType.VarChar).Direction = ParameterDirection.Output;
    sql_command.Parameters.Add("param_is_exist", MySqlDbType.Bit).Direction = ParameterDirection.Output;
 
    int result_rows = sql_command.ExecuteNonQuery();
 
    if (Convert.ToBoolean(sql_command.Parameters["param_is_exist"].Value) != true)
    {
        website_event_log.add_event_log("Membership", txtChannelName.Text.Trim(), "You have successfully signed up");
 
        Response.Redirect("~/Channels", true);
    }
    else
    {
        Session["message_title"] = "Error";
        Session["message_title_Color"] = Color.Red.ToArgb();
        Session["message_text"] = "The same channel address already exists. Please try another address.";
        Session["message_button_title"] = "Back";
        Session["message_button_url"] = Request.UrlReferrer.ToString();
 
        Response.Redirect("~/Message", false);
    }
/*
}
catch (Exception exp)
{
    Session["message_title"] = "ERROR..";
    Session["message_text"] = String.Format("Please try again.......{0}{1}", "<br /><br />", exp.Message);
    Session["message_button_title"] = "Back to Sign-in";
    Session["message_button_url"] = Request.UrlReferrer.ToString();
 
    Response.Redirect("~/Message", false);
}
finally
{
    // if (sql_reader != null) sql_reader.Close();
    if (sql_connection != null)
    {
        if (sql_connection.State == ConnectionState.Open)
            sql_connection.Close();
    }
}
    * */

 
Technology News @ www.JassimRahma.com

GeneralRe: webpage has resulted in too many redirects Pinmemberjkirkerx24 Dec '12 - 13:05 
You have to close the connection first before issuing a response.redirect, well you should at least, or else it can cause an error, or leave too many copies of the connector open. I would have to do research on that to confirm it, but I don't redirect when reading or writing data to a database.
 
Response.Redirect is the last thing you want to do in a sub, after closing and disposing of objects.
 
It's actually better to write a function without response.redirect, and have the function return a result or exit code, and then redirect.
 
 if (0 = createStoredProcedure()) then
   response.redirect("~/option")
 else
   response.redirect("~/error")
 End if

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 21 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid